13_类继承
本题考点:extends
根据题目要求,使”Chinese“类继承于”Human“类,核心步骤有:
- 在”Human“;类中添加”getName“函数
- 通过extends使”Chinese”类继承于“Human”类
- 在“Chinese”类的构造函数中可以通过super方法使“name”调用超类构造器
- 在“Chinese”类的构造函数中添加“age”属性
- 在“Chinese”类的构造函数中添加“getAge“函数
参考答案:
class Human {
constructor(name) {
this.name = name
this.kingdom = 'animal'
this.color = ['yellow', 'white', 'brown', 'black']
}
getName() {
return this.name
}
}
class Chinese extends Human{
constructor(name,age) {
super(name)
this.age = age
}
getAge() {
return this.age
}
}