首页 > 试题广场 >

继承

[编程题]继承
  • 热度指数:9926 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请补全JavaScript代码,实现以下功能:
1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
2. 将"Chinese"构造函数继承于"Human"构造函数
3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性
组合继承
function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}
Human.prototype.getName = function () {
    return this.name
}
function Chinese(name, age) {
    Human.call(this, name)
    this.age = age
    this.color = 'yellow'
}
// 组合继承
Chinese.prototype = new Human()
Chinese.prototype.constructor = Chinese
Chinese.prototype.getAge = function () {
    return this.age
}
let ans = function () {
    let me = new Chinese('me', 1);
    return me.getName() === 'me' && me.getAge() === 1
}
console.log(ans())
let c = new Chinese('12', 21)
console.log(c.getAge())
寄生组合继承
<!DOCTYPE html>
<html>

<head>
    <meta charset=utf-8>
</head>

<body>

    <script type="text/javascript">
        function Human(name) {
            this.name = name
            this.kingdom = 'animal'
            this.color = ['yellow', 'white', 'brown', 'black']
        }
        Human.prototype.getName = function () {
            return this.name
        }
        function Chinese(name, age) {
            Human.call(this, name)
            this.age = age
            this.color = 'yellow'
        }
        // 寄生组合继承
        (function () {
            var Super = function () { };
            Super.prototype = Human.prototype;
            Chinese.prototype = new Super()
        })()
        Chinese.prototype.getAge = function () {
            return this.age
        }
        let ans = function () {
            let me = new Chinese('me', 1);
            return me.getName() === 'me' && me.getAge() === 1
        }
        console.log(ans())
        let c = new Chinese('12', 21)
        console.log(c.getAge())
    </script>
</body>

</html>
发表于 2022-01-28 12:26:09 回复(0)
        <script type="text/javascript">
            function Human(name) {
                this.name = name
                this.kingdom = 'animal'
                this.color = ['yellow', 'white', 'brown', 'black']
            }
            
            function Chinese(name,age) {
                Human.call(this,name)
                this.age = age
                this.color = 'yellow'
            }
            // 补全代码
            Human.prototype.getName = function() {
                return this.name;
            }
            Chinese.prototype = new Human();
            Chinese.prototype.constructor = Chinese;
            Chinese.prototype.getAge = function() {
                return this.age;
            }
        </script>

发表于 2022-03-20 23:04:28 回复(0)
// 1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
Human.prototype.getName = function() {
    return this.name
}
// 2. 将"Chinese"构造函数继承于"Human"构造函数
Chinese.prototype = new Human()
Chinese.prototype.constructor = Chinese

// 3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性
Chinese.prototype.getAge = function() {
    return this.age
}

发表于 2022-03-12 12:49:48 回复(0)
<script type="text/javascript">
            function Human(name) {
                this.name = name
                this.kingdom = 'animal'
                this.color = ['yellow', 'white', 'brown', 'black']
            }
            
            function Chinese(name,age) {
                Human.call(this,name)
                this.age = age
                this.color = 'yellow'
            }

            // 补全代码
            Chinese.prototype = Object.create(Human.prototype)

            Human.prototype.getName = function (){
                console.log("this,name",this.name);
                return this.name
            }

            Chinese.prototype.getAge = function (){
                console.log("this,age",this.age);
                return this.age
            }
        </script>

发表于 2023-12-27 10:53:29 回复(0)
使用组合继承或寄生式组合继承均可,注意别忘记修改子构造函数的原型对象后,将原型对象的constructor属性指回构造函数。
function Human(name) {
        this.name = name
        this.kingdom = 'animal'
        this.color = ['yellow', 'white', 'brown', 'black']
    }
     
    function Chinese(name,age) {
        // 
        Human.call(this, name)
        this.age = age
        this.color = 'yellow'
    }
 
    // 补全代码
    Human.prototype.getName=function(){
        return this.name
    }

    // 以构造函数Human的原型对象为原型,创建新对象,将新对象作为构造函数Chinese的原型对象
    Chinese.prototype = Object.create(Human.prototype);
    
    // 构造函数Chinese的原型对象prototype的constructor属性,指回构造函数Chinese
    Chinese.prototype.constructor = Chinese;
 
    Chinese.prototype.getAge=function(){
        return this.age
    }


发表于 2023-12-11 17:38:26 回复(0)
<script type="text/javascript">
    function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}
Human.prototype.getName = function(){
    return this.name;
}

function Chinese(name,age) {
    Human.call(this,name)
    this.age = age
    this.color = 'yellow'
}
//最关键的一句
Chinese.prototype = Object.create(Human.prototype);
Chinese.prototype.getAge = function(){
    return this.age;
}

// 补全代码

</script>


发表于 2023-05-15 10:43:10 回复(0)
/ 补全代码
            Human.prototype.getName = function () {
            return this.name
        }
  //2. 将"Chinese"构造函数继承于"Human"构造函数
        Chinese.prototype.__proto__ = Human.prototype;
   
        Chinese.prototype.getAge = function () {
            return this.age
        }

发表于 2023-03-20 07:16:58 回复(0)
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            function Human(name) {
                this.name = name
                this.kingdom = 'animal'
                this.color = ['yellow', 'white', 'brown', 'black']
            }
            
            function Chinese(name,age) {
                Human.call(this,name)
                this.age = age
                this.color = 'yellow'
            }

            // 补全代码

            Human.prototype.getName = function(){
                return this.name
            }

            console.log(Chinese.prototype)
            Chinese.prototype = Human.prototype
            Chinese.prototype.constructor = Chinese
            Chinese.prototype.getAge = function (){
                return this.age
            }

           let me = new Chinese("yaohf",21)
           console.log( me.getAge())
           console.log(me.getName())
        </script>
    </body>
</html>

发表于 2023-03-10 16:52:37 回复(0)
     // 补全代码
Human.prototype.getName=function(){
    return this.name;
}

function F(){};
F.prototype = Human.prototype;
let instF = new F();
instF.constructor = Chinese;
Chinese.prototype = instF;

Chinese.prototype.getAge=function(){
    return this.age;
}

发表于 2022-12-03 21:41:34 回复(0)
组合继承
  • 用原型链实现对原型属性和方法的继承
  • 借用构造函数技术来实现实例属性的继承。
    function Human(name) {
        this.name = name
        this.kingdom = 'animal'
        this.color = ['yellow', 'white', 'brown', 'black']
    }
    
    function Chinese(name,age) {
        //继承属性
        // 第二次调用Human
        Human.call(this,name)
        this.age = age
        this.color = 'yellow'
    }
    
    Human.prototype.getName = function() {
        return this.name;
    }
    // 继承方法-构造原型链
    // 第一次调用Human
    Chinese.prototype = new Human();
    // 重写SubType.prototype的constructor属性,指向自己的构造函数SubType
    Chinese.prototype.constructor = Chinese;
    Chinese.prototype.getAge = function() {
        return this.age;
    }
    寄生组合式继承
  • 结合借用构造函数传递参数和寄生模式实现继承
function inheritPrototype(subType, superType){
    var prototype = Object.create(superType.prototype); // 创建对象,创建父类原型的一个副本
    prototype.constructor = subType;                    // 增强对象,弥补因重写原型而失去的默认的constructor 属性
    subType.prototype = prototype;                      // 指定对象,将新创建的对象赋值给子类的原型
}

function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}

function Chinese(name,age) {
    Human.call(this,name)
    this.age = age
    this.color = 'yellow'
}


Human.prototype.getName = function() {
    return this.name;
}
inheritPrototype(Chinese, Human);
Chinese.prototype.getAge = function() {
    return this.age;
}


发表于 2022-10-06 23:02:04 回复(0)
function Human(name) {
      this.name = name
      this.kingdom = 'animal'
      this.color = ['yellow', 'white', 'brown', 'black']
    }
    Human.prototype.getName = function () {
      return this.name
    }
    function Chinese(name, age) {
      Human.call(this, name)
      this.age = age
      this.color = 'yellow'
    }
    // 补全代码
    Chinese.prototype = new Human();
    Chinese.prototype.constructor = Chinese;
    Chinese.prototype.getAge=function(){
      return this.age
    }

发表于 2022-09-25 15:45:03 回复(0)
<html> <body>
    <script type="text/javascript">
        function Human(name) {
            this.name = name
            this.kingdom = 'animal'
            this.color = ['yellow', 'white', 'brown', 'black']
        }

        function Chinese(name,age) {
            Human.call(this,name)
            this.age = age
            this.color = 'yellow'
        }
        // 补全代码
        Human.prototype.getName = function() {
            return this.name;
        }
        Chinese.prototype = new Human();
        Chinese.prototype.constructor = Chinese;
        Chinese.prototype.getAge = function() {
            return this.age;
        }
    </script>
</body>
</body></html>
发表于 2022-08-30 11:17:10 回复(0)
这样也可以,但是我知道这样不合理。有没有大佬赐教,不合理的地方在哪里?
<script type="text/javascript">
    function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}
Human.prototype.getName = function(){return this.name;}

function Chinese(name,age) {
    Human.call(this,name)
    this.age = age
    this.color = 'yellow'
}

// 补全代码
Chinese.prototype = Human.prototype;
Chinese.prototype.getAge = function(){return this.age;}
</script>


发表于 2022-08-10 14:42:37 回复(2)
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
    }
}
跟第13题有什么不一样吗?
发表于 2022-07-13 20:00:09 回复(1)
          //1.
            Human.prototype.getName=function(){
                return this.name
            }


            // 2.
            Chinese.prototype = new Human()
            Chinese.prototype.constructor = Chinese
            //3.
            Chinese.prototype.getAge = function(){
                return this.age
            }
            

发表于 2022-06-22 17:10:02 回复(0)
Human.prototype.getName = function()  {
                return this.name
            }
            Chinese.prototype.__proto__ = Human.prototype;
            Chinese.prototype.getAge = function() {
                return this.age
            }
发表于 2022-04-28 11:49:46 回复(0)
 Human.prototype.getName = function(){
                return this.name
            }
            Chinese.prototype = new Human()
            Chinese.prototype.constructor = Chinese
            Chinese.prototype.getAge = function(){
                return this.age
            }
发表于 2022-03-29 15:29:20 回复(0)
Human.prototype.getName = function () {
  return this.name;
};
Chinese.prototype = Object.create(Human.prototype);
Human.prototype.getAge = function () {
  return this.age;
};
发表于 2022-03-27 10:06:33 回复(0)
这样为什么不行啊?
    <script type="text/javascript">
            function Human(name) {
                this.name = name
                this.kingdom = 'animal'
                this.color = ['yellow', 'white', 'brown', 'black']
            }
            Human.prototype.getName = function(){
                return this.name;
            }
            
            function Chinese(name,age) {
                Human.call(this,name)
                this.age = age
                this.color = 'yellow'
            }
            
            Chinese.prototype.getAge = function(){
                return this.age;
            }
        </script>


发表于 2022-02-22 20:51:43 回复(2)
<script type="text/javascript">
    function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}

function Chinese(name,age) {
    Human.call(this,name)
    this.age = age
    this.color = 'yellow'
}

// 补全代码
Human.prototype.getName = function(){
    return this.name;
} 
Chinese.prototype = new Human();
Chinese.prototype.constructor = Chinese;
Chinese.prototype.getAge=function(){
    return this.age;
}
</script>

发表于 2022-02-11 14:00:47 回复(0)