如何在Dart中编写静态构造函数?
class Generator
{
static List<Type> typesList = [];
//static
//{ /*static initializations*/}
}
如何在Dart中编写静态构造函数?
class Generator
{
static List<Type> typesList = [];
//static
//{ /*static initializations*/}
}
Dart中没有静态构造函数.命名构造函数(如Shape.circle()
)是通过以下方式实现的
class A {
A() {
print('default constructor');
}
A.named() {
print('named constructor');
}
}
void main() {
A();
A.named();
}
你可能也对这个factory constructors question感兴趣
Update:几个静电初始化器解决方法
class A {
static const List<Type> typesList = [];
A() {
if (typesList.isEmpty) {
// initialization...
}
}
}
或者,如果静电内容不允许类的用户访问,可以将其移出类.
const List<Type> _typesList = [];
void _initTypes() {}
class A {
A() {
if (_typesList.isEmpty) _initTypes();
}
}