ES6(七) 类的继承

类的继承

  • 类的继承和封装 类必须有构造函数这个属性
class Person {
    constructor({ name }) {
        this.name = name;
    }
    drive() {
        return "人可以看见心";
    }

}

const xiaoming = new Person({ name: "张三" });
console.log(xiaoming.name);
console.log(xiaoming.drive());
  • 上面那个是 ES6 写法,写一下注释
let a = {
    name() { return "a的名字" }
}
let b = {
    name: function() { return "b的名字" }
}
console.log(a.name());
console.log(b.name());
  • 类的继承和封装
class Person {
    constructor(option) {
        this.title = option.title
    }
    drive() {
        return "这就是人"
    }
}
class Man extends Person { //它继承Person
    constructor(option) {
        super(option); //继承Person的属性
        this.color = option.color
    }
}
const man = new Man({ color: "red", title: "名人" });

console.log(man.color);
console.log(man.title);
console.log(man.drive());

文章作者: 雾烟云
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 雾烟云 !
  目录