zoukankan      html  css  js  c++  java
  • 继承

    function Person(name,age,sex) {
    this.name=name;
    this.age=age;
    this.sex=sex;
    }
    Person.prototype.eat=function () {
    console.log("吃好吃的");
    };
    Person.prototype.play=function () {
    console.log("玩好玩的");
    };
    function Student(score) {
    this.score=score;
    }
    //改变学生的原型指向即可======》学生和人的关系已发生
    Student.prototype=new Person("小明",10,"男");
    Student.prototype.study=function () {
    console.log("学习很容易掉头发的。。。。");
    };
    //相同的代码太多,造成了代码的重复
    var stu=new Student(100);
    console.log(stu.name);
    console.log(stu.age);
    console.log(stu.sex);
    stu.eat();
    stu.play();
    console.log("下面是学生对象中自己有的");
    console.log(stu.score);
    stu.study();



    案例:
    * 动物有名字,有体重,有吃东西行为
    * 小狗有名字,有体重,有毛色,有吃东西行为,还有咬人行为
    * 哈士奇有名字,有体重,有毛色,性别,有吃东西行为,咬人行为和逗主人开心
    * */
    //动物的构造函数
    function Aimal(name,weight) {
    this.name=name;
    this.weight=weight;
    }
    //动物的原型方法
    Aimal.prototype.eat=function () {
    console.log("除了吃,还是吃");
    };
    //小狗的构造函数
    function Dog(color) {
    this.color=color;
    }
    //小狗的原型方法
    Dog.prototype=new Aimal("哮天犬","50");
    Dog.prototype.bitePerson=function () {
    console.log("汪汪汪,我会咬人的");
    };
    //哈士奇的构造函数
    function ErHa(sex) {
    this.sex=sex;
    }
    ErHa.prototype=new Dog("灰白色");
    ErHa.prototype.playHost=function () {
    console.log("我会咬鞋子,衣服还有沙发");
    };
    var reHa=new ErHa("雄性");
    console.log(reHa.name,reHa.weight,reHa.color);
    reHa.eat();
    reHa.bitePerson();
    reHa.playHost();
  • 相关阅读:
    杭电OJ 输入输出练习汇总
    七月读书笔记
    情报分析报告阅读笔记
    情报研究与分析入门阅读笔记
    旁观者攻击
    域前置技术相关学习
    CC攻击和C2的区别
    DNS投毒学习分析总结
    数字证书2.0版本学习总结
    《在树洞里》-感悟
  • 原文地址:https://www.cnblogs.com/lujieting/p/10067305.html
Copyright © 2011-2022 走看看