1.类的继承
// 1.类的继承
class Father{
constructor(x,y){
this.x = x;
this.y = y;
}
sum(){
console.log(this.x + this.y);
}
}
class Son extends Father{
// 1.这种方法是行不通的,这个只关乎到子类的构造函数,和父级无关
// constructor(x,y){
// this.x = x;
// this.y = y;
// }
// 2.super关键字用于访问和调用对象父类上的函数。可以调用父类的构造函数,也可以调用父类的普通函数
constructor(x,y){
super(x,y); //调用了父类中的构造函数
}
}
var son = new Son(1,2);
son.sum();
2.子类继承父类 加法方法 同时扩展 减法方法
class Father {
constructor(x,y){
this.x = x;
this.y = y;
}
sum(){
console.log(this.x + this.y);
}
}
//子类继承父类 加法方法 同时扩展 减速方法
class Son extends Father {
constructor(x,y){
// 利用super 调用父类的构造函数
// super 必须在子类this之前调用
super(x,y);
this.x = x;
this.y = y;
}
substract(){
console.log(this.x - this.y);
}
}
var son = new Son(5,3);
son.substract(); // 2
son.sum();//8