微信小游戏基础(二) - 继承

好处:可以提高代码的复用性

使用关键字extends,extends的意思是扩展

class People{

constructor(name){

this.name = name;

}

eat(){

console.log("我能吃");

}

run(){

console.log("我能跑");

}

}

class Son extends People(){

}

var son = new Son("张三");

son.eat();

子类继承了父类的属性和方法,但由于没有部署代码,所以这两个类完全一样

注:子类必须在构造方法中调用supper方法,否则创建对象会报错。这是由于子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工,如果不调用supper,子类就达不到this对象,如果子类没有构造方法,这个方法会被默认添加

猜你喜欢

转载自blog.csdn.net/qq_38845858/article/details/82936653