dart - Dart 中的静态构造函数
问题描述
如何在 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();
}
您可能还对这个工厂构造函数问题感兴趣
更新:几个静态初始化器变通办法
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();
}
}
推荐阅读
- python - 以文本形式获取 ascii 响应,如何在 Python 中解码
- c++ - C ++ QT在类结构中传递更高消息的最佳方法
- javascript - JS:在叠加层中打开随机视频、图像或链接
- jenkins - 通过 jenkins 上传的 JFrog Artifactory 工件不会出现在构建中
- arrays - 如何在 Ruby 中使用 %w 中的常量?
- spring-boot - 如何在使用 io.confluent 提供的 kafka-schema-registry-maven-plugin 时加密 userInfoConfig 参数?
- android - 为什么在 Android Studio 中从 Firebase 实时数据库读取数据如此缓慢?
- crystal-lang - 在这种情况下,我如何处理好 nil ?
- html - 如何以角度循环数据?
- php - 问题是什么?dd($数据);它不返回任何东西