dart 库和可见性
示例
与Java,飞镖不具备关键字public,protected和私有。如果标识符以下划线开头_,则它是其库的私有标识符。
例如,如果您在单独的库文件(例如other.dart)中拥有类A,则:
library other;
class A {
int _private = 0;
testA() {
print('int value: $_private'); //0
_private = 5;
print('int value: $_private'); //5
}
}然后将其导入您的主应用程序,例如:
import 'other.dart';
void main() {
var b = new B();
b.testB();
}
class B extends A {
String _private;
testB() {
_private = 'Hello';
print('String value: $_private'); //你好
testA();
print('String value: $_private'); //你好
}
}您将获得预期的输出:
String value: Hello int value: 0 int value: 5 String value: Hello