es6的类和继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal{
// 静态方法(静态方法只能通过类名调用,不可以使用实例对象调用)
static showInfo(){
console.log('hi');
}
// 构造函数
constructor(name){
this.name = name;
}
showName(){
console.log(this.name);
}
}
let a = new Animal('spike');
a.showName();
a.showInfo();
Animal.showInfo();

类的继承extends

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();