语法: 若无继承单看父类
父类
class People {
// 构造器
constructor (name, age) {
this.name = name;
this.age = age;
}
// 实例方法
eat () {
console.log('吃吃吃');
}
// 类方法
static create () {
console.log('诞生');
}
}
子类
class Student extends People {
constructor (name, age) {
// super关键词
super(name, age)
}
learn (){
console.log(this.name + 'learn')
}
}
实例化对象
var p1= new People('人类',18);
var s1= new Student('学生',28);
父类使用父类的方法
p1.eat();
子类使用父类方法
s1.eat();
子类使用自身方法
s1.learn();