Dart 不允许您在运行时从类的实例中添加或删除成员变量。在 Dart 中重写您的示例,它可能看起来像这样:
class doStuff {
bool isDefined;
doStuff() {
isDefined = true;
}
void stuff() {
print('The function stuff was called!');
}
}
main() {
new doStuff().stuff();
}
如果你想在 Dart 的类中添加一个属性包,你可以这样写:
class PropertyObject {
Map<String, Dynamic> properties;
PropertyObject() {
properties = new Map<String, Dynamic>();
}
Dynamic operator[](String K) => properties[K];
void operator[]=(String K, Dynamic V) => properties[K] = V;
}
main() {
PropertyObject bag = new PropertyObject();
bag['foo'] = 'world';
print('Hello ${bag['foo']}');
}
请注意,您不能使用“.”访问地图属性。操作员。