//父类 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()
原文:https://www.cnblogs.com/dahai5566/p/13503882.html