不適用於地圖文字。但是你可以製作自己的地圖類,並且你可以通過多種方式來實現這一點。
同方法
import "dart:collection";
class MyMap extends LinkedHashMap {
String modifiedName() {
String name = this["name"];
return "$name <- modified";
}
}
void main() {
MyMap map = new MyMap();
map["name"] = "John";
print(map.modifiedName());
map["name"] = "Harry";
print(map.modifiedName());
}
作爲財產
import "dart:collection";
class MyMap extends LinkedHashMap {
String _modifiedName() {
String name = this["name"];
return "$name <- modified";
}
String get modifiedName => _modifiedName();
}
void main() {
MyMap map = new MyMap();
map["name"] = "John";
print(map.modifiedName);
map["name"] = "Harry";
print(map.modifiedName);
}
作爲地圖的關鍵
import "dart:collection";
class MyMap extends LinkedHashMap {
String operator [](String key) {
if(key == "modifiedName") {
String n = this["name"];
return "$n <- modified";
}
return super[key];
}
}
void main() {
MyMap map = new MyMap();
map["name"] = "John";
print(map["modifiedName"]);
map["name"] = "Harry";
print(map["modifiedName"]);
}
是否有你不想爲此創建Dart類的原因? – 2013-04-26 00:26:38