class Father {
constructor(x, y) {
this.x = x;
this.y = y;
}
sum() {
return this.x + this.y;
}
}
class Son extends Father {
subtract() {
return this.x - this.y;
}
}
var a = new Son(1, 1);
console.log(a.sum());
console.log(a.subtract());
这是没有加的控制台输出正常
class Father {
constructor(x, y) {
this.x = x;
this.y = y;
}
sum() {
return this.x + this.y;
}
}
class Son extends Father {
constructor(x, y) {
super(x, y);
}
subtract() {
return this.x - this.y;
}
}
var a = new Son(1, 1);
console.log(a.sum());
console.log(a.subtract());
这是加过了之后,控制台输出依然正常
constructor不写的话,子类也会把参数传给父类。如果写了constructor的话,super就一定要写了,不然参数无法传递给父类。
可以写constructor 也可以不写,你不写 它默认会自动有 。但是写了 就必须写全