13_类继承

本题考点:extends

根据题目要求,使”Chinese“类继承于”Human“类,核心步骤有:

  1. 在”Human“;类中添加”getName“函数
  2. 通过extends使”Chinese”类继承于“Human”类
  3. 在“Chinese”类的构造函数中可以通过super方法使“name”调用超类构造器
  4. 在“Chinese”类的构造函数中添加“age”属性
  5. 在“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
    }
}