我想创建一个私有变量,但我不能。
这是我的代码:
void main() {
var b = new B();
b.testB();
}
class A {
int _private = 0;
testA() {
print('int value: $_private');
_private = 5;
}
}
class B extends A {
String _private;
testB() {
_private = 'Hello';
print('String value: $_private');
testA();
print('String value: $_private');
}
}
当我运行这段代码时,会得到以下结果:
String value: Hello
int value: Hello
Breaking on exception: type 'int' is not a subtype of type 'String' of 'value'.
另外,我没有得到任何错误或警告时,编辑这个源代码。
如何在 Dart 中创建私有变量?