在子类构造器中调用父类的构造器实例父类
涉及关键字:extends super
案例:
// 创建父类 -Prenson
class Preson{
constructor(name,age){
this.name = name
this.age = 18
}
say(){
console.log(this.name,'说话')
}
}
// 创建子类-STudent
class Student extends Preson{ // 继承父类
constructor(name,age,num){
super(name,age) // 关键字super实例父类
this.num = num
}
readBook(){
console.log('读书!!')
}
}
// 结果输出
let s1 = new Student('bob',18,10001)
s1.readBook()
s1.say()
console.log(s1.name,s1.age,s1.num)
结果打印:
读书!!
bob 说话
bob 18 10001