class可以通过extends关键字实现继承,子类可以没有构造函数,系统会默认分配。子类提供了构造函数则必须要显示调用super。super函数类似于借用构造函数。类似于Animal.call()
1.子类对象指向父类对象
2.子类原型对象继承父类原型对象
class Animal(){
// 静态属性
static animalAttr='Animal的静态属性';
constructor(name,age,weight){
this.name=name;
this.age=age;
this.weight=weight;
}
// 实例方法
sayName(){
console.log('实例方法')
}
// 静态方法
static animalmethod(){
console.log('Animal静态方法')
}
}
// 要实现继承
class Dog extends Animal{
constructor(name,age,weight,color){
super(name,age,weight);
this.color=color;
console.log('Dog的构造器')
}
}