首页 > 其他 > 详细

ES6 class

时间:2020-08-14 20:28:18      阅读:46      评论:0      收藏:0      [点我收藏+]
//父类
class People {
    constructor(name) {
        this.name = name
    }
    eat() {
        console.log(`eat: 姓名 ${this.name}`)
    }
}
//子类
class Student extends People {
    constructor(name, number) {
        super(name)
        this.number = number
    }
    sayHi() {
        console.log(
            `sayHi: 姓名 ${this.name} , 学号 ${this.number}`
        )
    }
}
//子类
class Teacher extends People {
    constructor(name, major){
        super(name)
        this.major = major
    }
    teach(){
        console.log(
            `teach: 姓名 ${this.name} , 教授 ${this.major}`
        )
    }
}

// 通过类 new 对象/实例
const xiaoming = new Student(‘小明‘, ‘100‘)
console.log(xiaoming.name)
xiaoming.eat()
xiaoming.sayHi()

const zhanglaoshi = new Teacher(‘张老师‘, ‘语文‘)
zhanglaoshi.eat()
zhanglaoshi.teach()

 

ES6 class

原文:https://www.cnblogs.com/dahai5566/p/13503882.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!