首页 > 试题广场 >

类继承

[编程题]类继承
  • 热度指数:12395 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请补全JavaScript代码,完成类的继承。要求如下:
1. "Chinese"类继承于"Human"类
2. "Human"类实现一个函数"getName",返回该实例的"name"属性
3. "Chinese"类构造函数有两个参数,分别为"name"、"age"
4. "Chinese"类实现一个函数"getAge",返回该实例的"age"属性
关键:extends关键字、constructor关键字、super()方法
        <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) {
                    // 从父类集成name属性
                    super(name);
                    this.age = age;
                }
                // getAge方法
                getAge() {
                    return this.age;
                }
                
            }
        </script>



发表于 2022-03-20 22:03:32 回复(0)
class Human {
    constructor(name) {
        this.name = name
        this.kingdom = 'animal'
        this.color = ['yellow', 'white', 'brown', 'black']
    }
    // 补全代码
}
//方法通过原型链也是可以的
Human.prototype.getName = function(){
    return this.name;
}

// 补全代码
class Chinese extends Human {
    constructor(name,age){
        super(name);
        this.age = age;
    }
    //不要写成function getAge(){return this.age}
    getAge(){
        return this.age;
    }
}

编辑于 2024-03-26 02:52:59 回复(1)
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
                }
                
                
                
            }
发表于 2023-03-10 16:20:05 回复(0)
<!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
                }

            }

            new Chinese("yaohf",32)
        </script>
    </body>
</html>

发表于 2023-03-09 23:19:06 回复(0)
貌似写多了
  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
                }
                getName(){
                    return super.getName()
                }
            }


发表于 2023-02-13 13:09:05 回复(0)
                // 补全代码
                getName() {
                    return this.name
                }
            }

            // 补全代码
            class Chinese extends Human{
                constructor(name,age) {
                    super(name)
                    this.age = age
                }
                getAge() {
                    return this.age
                }
            }

发表于 2022-12-25 12:20:01 回复(0)
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
            }
        }
发表于 2022-12-18 22:09:11 回复(0)
<!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
                }
            }

            // 补全代码 super调用父级,extends继承
            class Chinese extends Human{
                constructor(name, age){
                    super(name)
                    this.age = age
                }
                getAge() {
                    return this.age
                }
            }
        </script>
    </body>
</html>

发表于 2022-12-17 16:43:40 回复(0)
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
    }
}

发表于 2022-12-03 18:32:53 回复(0)
继承 extends      构造器constructor    super 调用父类的构造函数 
   <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>
发表于 2022-11-21 12:05:39 回复(0)
<!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>

发表于 2022-09-12 09:27:23 回复(0)
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.name = name
        this.age = age
    }
    getAge() {
        return this.age
    }
}

发表于 2022-07-12 22:28:43 回复(0)
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
    }

}

发表于 2022-06-28 13:59:31 回复(0)
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
                this.name = name;
            }
            getAge() {
                return this.age;
            }
        }

发表于 2022-03-06 17:49:37 回复(0)
<!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,age)
                      this.age = age
                  }
                getAge(){
                    return this.age
                }
            }
        </script>
    </body>
</html>

发表于 2022-02-19 14:20:01 回复(0)
<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>
发表于 2022-01-30 00:56:42 回复(0)

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
  }
}
发表于 2022-01-11 18:00:51 回复(0)
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
    }
}

发表于 2021-12-07 18:09:07 回复(0)