es6类的定义与继承

文章目录

1. 定义类

构造函数(Constructor)在对象创建或者实例化时候被调用的方法。通常使用该方法来初始化数据成员和所需资源。构造器Constructor在js不能被继承,因此不能重写Overriding,但可以被重载Overloading

class Person {
    constructor(name) {
        this.name = name;
        this.sex = '男'
        this.money = true
    }
    getName() {
        // this 指向Person类
        console.log(this)
        console.log(`姓名:${this.name}`)
    }
    setName(name) {
        this.name = name
    }
}

let p1 = new Person("doudou");

// p1.setName('流浪')
p1.getName()
2. 继承
class Father {
    constructor(name) {
        this.name = name;
        this.sex = '男'
        this.money = true
    }
    getName() {
        console.log(this)
        console.log(`姓名:${this.name}`)
    }
    setName(name) {
        this.name = name
    }
}

class Son extends Father {
    constructor(name) {
        super(name)
    }
    getInfo() {
        console.log(this)
    }
    static people() {
        console.log('这个是es6中的静态方法;')
    }
}

let s4 = new Son('doudou')
s4.getInfo()
Son.people()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值