访问dart中的继承字段

时间:2013-06-06 21:31:32

标签: dart

abstract class Painter {

    CanvasElement canvas;

    Painter(this.canvas);

    void draw();
}

class SpritePainter extends Painter{

    SpritePainter(this.canvas);

    void draw(){
        window.console.log("Drawing");
        window.console.log(canvas);
    }

}

使用上面的代码,我的应用程序在尝试调用new SpritePainter(query('#sprite-canvas'));时说this.canvas是一个未知字段时失败了。我认为抽象父类中的CanvasElement可以被子类访问吗?

更新

我修复了这个问题:

SpritePainter(CanvasElement canvas):super(canvas);

但后来我读到了dart教程,抽象类只能有工厂构造函数吗?

1 个答案:

答案 0 :(得分:6)

您必须将您的参数转发给超级构造函数,如下所示:

abstract class Painter {
  CanvasElement canvas;

  Painter(this.canvas);

  void draw();
}

class SpritePainter extends Painter{
  SpritePainter(CanvasElement canvas) : super(canvas);

  void draw(){
    window.console.log("Drawing");
    window.console.log(canvas);
  }
}