ES6语法的类与继承
程序员文章站
2023-12-21 13:11:04
...
ES6语法的类与继承
class Animal{
// 静态方法(静态方法只能通过类名调用,不可以使用实例对象调用)
static showInfo(){
console.log('hi');
}
// 构造函数
constructor(name){
//对象属性
this.name = name;
}
//对象方法
showName(){
console.log(this.name);
}
}
// 类的继承extends
class Dog extends Animal{
constructor(name,color){
super(name);//注意super用来调用父类
this.color = color;
}
showColor(){
console.log(this.color);
}
}
let d = new Dog('doudou','yellow');
d.showName();
d.showColor();
// d.showInfo();
Dog.showInfo();