我玩Dart(通过Dart语言之旅),我发现我不能在子类上使用初始化列表。为什么呢?
main() {
var rbt = new Robot.fromJson({'x':21, 'y':21});
}
class Human {
}
class Robot extends Human {
int x;
int y;
Robot.fromJSON(Map map) : x = map['x'], y = map['y'] {
print('Robot location is $x, $y');
}
}
导致错误:
Exception: No constructor 'Robot.fromJson' declared in class 'Robot'.
NoSuchMethodError: method not found: 'Robot.fromJson'
Receiver: Type: class 'Robot'
Arguments: [Instance of '_LinkedHashMap']
答案 0 :(得分:3)
Dart区分大小写
fromJSON
vs fromJson