最简单的用法 class Student extends Person {}
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
say() {
console.log('hello');
}
}
class Student {}
class Student extends Person {
}
let s1 = new Student()
此时class Student { } 中什么都不写,
但是s1中,已经可以使用用Person构造函数的constructor函数和一般函数了
如果有super用法 class Student extends Person {}
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
say() {
console.log('hello');
}
}
class Student {
constructor(name, age,grade) {
//super相当于调用父类(Person)的constructor函数,把父类constructor函数的内容复制到子类中
//super必须在最前
super(name,age)
this.grade = grade
}
// 子类如果有和父类相同的方法,会使用子类的,不会再去访问父类的方法了
say() {
}
}
class Student extends Person {
}
let s1 = new Student()
super相当于调用父类(Person)的constructor函数,把父类constructor函数的内容复制到子类中 super必须在最前 子类如果有和父类相同的方法,会使用子类的,不会再去访问父类的方法了