题解 | #类继承#

类继承

http://www.nowcoder.com/practice/c06b31bf07b94c5f9dbef9645e97ea28

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
    }
}

全部评论

相关推荐

评论
16
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务