- 通过class定义类/实现类的继承
- 在类中通过constructor定义构造方法
- 通过new来创建类的实例
- 通过extends来实现类的继承
- 通过super调用父类的构造方法
- 重写从父类中继承的一般方法
function Person(name, age){
this.name = name;
this.age = age;
}
let person = new Person('kobe', 40);
console.log(person);
//定义一个人物的类
class Person{
// 类的构造方法
constructor(name, age){
this.name = name;
this.age = age;
}
// 类的一般方法
showName(){
console.log('调用父类的方法');
console.log(this.name, this.age);
}
}
let person = new Person('kobe', 40);
console.log(person);
// person.showName();
// 子类
class StarPerson extends Person{
constructor(name, age, salary){
super(name, age);
this.salary = salary;
}
// 父类的方法重写
showName(){
console.log('调用子类的方法');
console.log(this.name, this.age, this.salary);
}
}
let sp = new StarPerson('wade', 36, 152450000);
console.log(sp);
sp.showName();
网友评论