[ class ] 類的繼承

類的繼承

思路 :
一個對象可以繼承另一對象身上的屬性和方法 !

class People {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    //父級的靜態方法
    static hello() {
        console.log('hello world');
    }
    p() {
        return 2;
    }
}
class Animal extends People {
    constructor(name, age, skill) {
        super(name, age);
        this.skill = skill;
    }
    p() {
        return super.p();
    }
}

var animal = new Animal('judy', '18', 'web');
var wayne = new People('wayne', 88)

console.log(animal);
//[object Object] {
//   age: "18",
//   name: "judy",
//   skill: "web"
// }
console.log(Animal.hello()) //"hello world" 直接調用父級的靜態方法
console.log(animal.p()) //2


console.log(wayne)
// [object Object] {
//   age: 88,
//   name: "wayne"
// }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章