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教程,抽象类只能有工厂构造函数吗?
答案 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);
}
}