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>

  • 相关阅读:
    JAVA中的super和this关键字的使用
    JAVA中类以及成员变量和成员方法的修饰符的总结
    JAVA中的抽象类和接口
    JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作
    完整日期正则表达式
    2017实习【Java研发】面经
    MySQL事务及隔离级别(读书小结)
    Java类编译、加载、和执行机制
    JVM内存回收机制
    Centos6.5的MySQL5.7.15二进制源码单机版安装
  • 原文地址:https://www.cnblogs.com/zengjie123/p/4660229.html
Copyright © 2011-2022 走看看