题解 | #类继承#

类继承

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

<!-- 
    描述
    请补全JavaScript代码,完成类的继承。要求如下:
        1. "Chinese"类继承于"Human"类
        2. "Human"类实现一个函数"getName",返回该实例的"name"属性
        3. "Chinese"类构造函数有两个参数,分别为"name"、"age"
        4. "Chinese"类实现一个函数"getAge",返回该实例的"age"属性
 -->
 <!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            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
                }
            }
        </script>
    </body>
</html>

KK学习前端之js答题过程 文章被收录于专栏

记录自己学习前端的各个过程,定期复盘,努力提高自己。 此专栏为自己的js答题记录。 欢迎各位一起沟通与讨论。

全部评论

相关推荐

2025-12-16 15:40
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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