void main(){
new C(new A()); // This gives no warning or error
new D(new A()); // This gives an error
}
class A{}
class B extends A{}
class C {
B b;
C(A bb){ this.b = bb; }
}
class D {
B b;
D(this.b);
}
在main函数的两个语句中,我给出了一个类型为A的实例作为参数。如果我没有错,两个语句都应该在强模式下给出错误或警告,但只有第二个给出错误:类型检查失败:新的A()(A)不属于B
Image showing the code compiled from https://dartpad.dartlang.org/
我刚开始学习dart语言,我无法在文档中找到对此案例的解释。有人知道为什么会这样吗?
答案 0 :(得分:1)
看起来你在C
构造函数中输入了一个拼写错误。你的意思是:
class C {
B b;
C(B/*not A*/ bb){ this.b = bb; }
}