2
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);

但是后来我在飞镖教程上读到抽象类只能有工厂构造函数?

4

1 回答 1

8

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

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);
  }
}
于 2013-06-06T21:35:54.793 回答