相关疑难解决方法(0)

如何在Dart中创建私有变量?

我想创建一个私有变量,但我不能.

这是我的代码:

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');
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我得到以下结果:

String value: Hello
int value: Hello
Breaking on exception: type 'int' is not a subtype of type 'String' of 'value'.
Run Code Online (Sandbox Code Playgroud)

编辑此源代码时,我也没有收到任何错误或警告.

如何在Dart中创建私有变量?

dart

48
推荐指数
4
解决办法
2万
查看次数

标签 统计

dart ×1