js面向对象之继承-原型继承

复制代码
//animal 父类 超类
        var Animal = function(name)
        {
            this.name = name;
            this.sayhello = function()
            {
                alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
            };
        };
        Animal.prototype.shout = function()
        {
            alert(this.name + ",正在叫!");
        };
        Animal.prototype.game = function()
        {
            alert(this.name + ",正在玩耍!");
        };
        
        var Dog = function(name)
        {
            //this.name = name;
            this.name = name;
            this.shout = function()//重写父类的函数
            {
                alert(this.name + ",正在汪汪叫!");
            };
        };
        
        var Cat = function(name)
        {
            this.name = name;
            this.shout = function()
            {
                alert(this.name + ",正在喵喵叫!");
            };
        };
        
        //原型继承
        Dog.prototype = Cat.prototype = new Animal();
        
        var xh = new Dog("小黑");
        xh.sayhello();
        xh.shout();
        xh.game();
        
        var xm = new Cat("小咪");
        xm.sayhello();
        xm.shout();
        xm.game();
复制代码

 封装一个函数后:

复制代码
var inherit = function (subclass, superclass) {
    subclass.prototype = new superclass();
};
 
//animal 父类 超类
var Animal = function (name) {
    this.name = name;
    this.sayhello = function () {
        alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
    };
};
Animal.prototype.shout = function () {
    alert(this.name + ",正在叫!");
};
Animal.prototype.game = function () {
    alert(this.name + ",正在玩耍!");
};
 
var Dog = function (name) {
    //this.name = name;
    this.name = name;
    this.shout = function () //重写父类的函数
    {
        alert(this.name + ",正在汪汪叫!");
    };
};
 
var Cat = function (name) {
    this.name = name;
    this.shout = function () {
        alert(this.name + ",正在喵喵叫!");
    };
};
 
//原型继承
//Dog.prototype = Cat.prototype = new Animal();
inherit(Dog, Animal);
inherit(Cat, Animal);
 
var xh = new Dog("小黑");
xh.sayhello();
xh.shout();
xh.game();
 
var xm = new Cat("小咪");
xm.sayhello();
xm.shout();
xm.game();
复制代码

给函数添加extends方法

复制代码
Function.prototype.extends = function (superclass) {
    this.prototype = new superclass();
};
 
//animal 父类 超类
var Animal = function (name) {
    this.name = name;
    this.sayhello = function () {
        alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
    };
};
Animal.prototype.shout = function () {
    alert(this.name + ",正在叫!");
};
Animal.prototype.game = function () {
    alert(this.name + ",正在玩耍!");
};
 
var Dog = function (name) {
    this.name = name;
    this.shout = function () //重写父类的函数
    {
        alert(this.name + ",正在汪汪叫,叫的很开心!");
    };
};
Dog.extends(Animal);
 
var Cat = function (name) {
    this.name = name;
    this.shout = function () {
        alert(this.name + ",正在喵喵叫!");
    };
};
Cat.extends(Animal);
 
//原型继承
//Dog.prototype = Cat.prototype = new Animal();
/*inherit(Dog, Animal);
        inherit(Cat, Animal);*/
//Dog.extends(Animal);
//Cat.extends(Animal);
 
/*var xh = new Dog("小黑");
        xh.sayhello();
        xh.shout();
        xh.game();
         
        var xm = new Cat("小咪");
        xm.sayhello();
        xm.shout();
        xm.game();*/
 
var Husky = function (name, color, sex) {
    this.name = name;
    this.color = color;
    this.sex = sex;
    this.sayhello = function () {
        alert("Hello,我是一条小" + this.sex + "狗,有一个非常好听的名字,叫:“" + this.name + "”,你愿意和我做朋友吗?");
    };
    this.showcolor = function () {
        alert(this.color);
    };
    /*this.shout = function()//重写父类的函数
            {
                alert(this.name + ",哼哼叫!");
            };*/
};
Husky.extends(Dog);
 
var xh = new Husky("小哈", "黑白", "公");
xh.sayhello();
xh.shout();
xh.game();
xh.showcolor();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值