美文网首页
(七)class

(七)class

作者: 我拥抱着我的未来 | 来源:发表于2018-06-13 09:21 被阅读0次

类的继承

  • 类的继承和封装 类必须有构造函数这个属性
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());

相关文章

网友评论

      本文标题:(七)class

      本文链接:https://www.haomeiwen.com/subject/nfideftx.html