zoukankan      html  css  js  c++  java
  • 函数的继承


    关于函数的继承,子类继承父类属性

    示例如下:
    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    </body>
    <script>
    function Animal(name,age){//最大父元素:动物
    this.name=name;//所拥有的两个特征name和age
    this.age=age;
    this.show=function(){
    console.log("动物的名字叫:"+this.name+" 动物的年龄为:"+this.age);
    }
    }

    function Dog(name,age,lei){//动物的子元素Dog
    //对象冒充法1
    //this.inhert = Animal;
    //this.inhert(name,age);
    //delete this.inhert;

    //对象冒充法2
    //Animal.call(this,name,age);

    //对象冒充法3
    //Animal.apply(this,[name,age]);

    //以上三种冒充法和下面真正继承法继承了动物的name和age特征
    继承了动物的name和age
    this.name = name;
    this.age = age;

    this.lei=lei;//同时添加lei的新特征
    }
    Dog.prototype = new Animal();//真正继承

    function Cat(name,age,color){//动物的子元素Cat同时是jiafei和jiqimao的父元素
    Animal.call(this,name,age);//继承了动物的name和age
    this.color=color;//同时添加color的新特征
    }


    function bomei(name,age,lei,tixing){
    Dog.call(this,name,age,lei);
    this.tixing=tixing
    }
    var bomei = new bomei("博美",5,"犬类","小");
    bomei.show();

    function hashiqi(name,age,lei,tixing){
    Dog.call(this,name,age);
    this.tixing=tixing

    }
    var hashiqi = new hashiqi("哈士奇",2,"犬类","大");
    hashiqi.show();


    function jiafei(name,age,color,food){
    Cat.call(this,name,age);
    this.food=food
    }
    var jafei=new jiafei("小家",3,"黄色","鱼")
    jafei.show();



    function jiqimao(name,age,color,gongneng){//继承了Cat的所有特征
    Cat.call(this,name,age,color);
    this.gongneng=gongneng;//添加新特征gongneng
    this.show1=function(){//建立函数
    console.log("动物的名字叫:"+this.name+" 动物的年龄为:"+this.age+"颜色:"+this.color+"功能:"+this.gongneng);
    }
    }
    //产生一个jiqimao的实例
    var jiqimao=new jiqimao ("多拉A梦",58,"蓝白","空间袋")
    jiqimao.show1();//调用show1()的函数
    </script>
    </html>

  • 相关阅读:
    Vue.js入门(3)TypeScript
    Dapper源码学习
    .Net中手动实现AOP
    .Net面向对象(OOP)
    Redis实战(18)Redis位图巧用,节约内存
    .Net深拷贝浅拷贝
    .NET面试题系列(22)字符串暂存池(缓冲池)
    .NET面试题系列(二十一)C#中Equals和==的比较
    C# 8.0
    C# 7.0
  • 原文地址:https://www.cnblogs.com/zengjie123/p/4660229.html
Copyright © 2011-2022 走看看