上代码
class Point extends Test {
num x;
num y;
num z;
num get a {
return this.x + this.y;
}
set a(num v) {
x = v - y;
}
Point.fromJson(Map jsonMap)
: this.x = jsonMap['x'],
y = jsonMap['y'],
super.fromJson({}) {
print(this.x);
print('In Point.fromJson(): ($x, $y, $z)');
}
Point.aliasB(Map jsonMap, num z)
: this.z = z,
super.fromJson({}) {
Point.fromJson(jsonMap);
}
Point(Map jsonMap, this.z) : super(jsonMap) {
print('In Point(): ($x, $y, $z)');
x = jsonMap['x'];
y = jsonMap['y'];
print('In Point(): ($x, $y, $z)');
}
}
class Test {
num x;
num y;
Test(Map map)
: x = map['x'],
y = map['y'] {
print('In Parent.Test(): ($x, $y)');
}
Test.fromJson(Map map) : this(map);
}
main(List<String> args) {
new Point({'x': 1, 'y': 2}, 3);
new Test({'x': 1, 'y': 2});
}
执行结果
In Parent.Test(): (null, null)
In Point(): (null, null, 3)
In Point(): (1, 2, 3)
In Parent.Test(): (1, 2)
疑问:
1:Test 构造函数冒号右边不是赋值了吗 为啥是 null ?接受的参数打印出来是有值的
2:如果我把 Test 类的成员变量 x,y 改名字不跟子类一样,就能打印出来,这又是为啥?
希望有大佬能解答一下菜鸟的疑问