[英]Dart: Why does the following code compiles in strong mode?
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);
}
在主函數的兩個語句中,我都將類型A的實例作為參數。如果我沒有記錯,兩個語句都應在“強”模式下給出錯誤或警告,但是僅第二個語句會給出錯誤:
該圖顯示了從https://dartpad.dartlang.org/編譯的代碼
我剛剛開始學習dart語言,但在文檔中找不到這種情況的解釋。 有人知道為什么會這樣嗎?
看起來您在C
構造函數中輸入了錯誤。 你的意思是:
class C {
B b;
C(B/*not A*/ bb){ this.b = bb; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.