您的位置:首页 > 移动开发 > Objective-C

[Object]继承(经典版)(四)多重继承和组合继承

2015-02-13 11:06 239 查看

[Object]继承(经典版)(四)多重继承和组合继承

博客分类:

Web前端-JS语言核心

多重继承

作者:zccst

一、多重继承

不得不说,这又是异常精彩的章节,是原型链的升华。

Js代码


//父类1

function Parent1(name,age){

this.name = name;

this.age = age;

this.height=180;

}

Parent1.prototype.say = function(){

alert('hi...');

}

//父类2

function Parent2(name,age,weight){

this.name = name;

this.age = age;

this.weight = weight;

this.height = 170;

this.skin='yellow';

}

Parent2.prototype.walk = function(){

alert('walk...');

}

//子类

function Child(name,age,weight){

Parent1.call(this,name,age);

Parent2.call(this,name,age,weight);

}

//建立联系

for(var i in Parent1.prototype){Child.prototype[i] = Parent1.prototype[i]}

for(var i in Parent2.prototype){Child.prototype[i] = Parent2.prototype[i]}

var c1 = new Child('xiaoming',10,8);

console.log(c1);

其实,多重继承才是真实的物理世界,有一位博友用多重继承实现了照片墙,甚是精彩。

可以说是受教了,开眼界了。

多重继承还是很强大的。

博主的主要思想是循环复制多个父类原型对象的属性和方法到子类的原型对象,这样子类就可以继承多个父类了。

照片墙是个很有说服力的例子。其关系是:

Photo //会选择,鼠标移上,鼠标离开

Child1 //继承Photo,重写鼠标移上,离开。目的是移上放大三倍,离开回到原来尺寸。

Child2 //继承Photo,实现旋转10-40度

Child3 //继承Child1和Child2,实现即放大三倍,有选择10-40度

二、组合继承

类似这样

function C(){

B.call(this, xx);

A.call(this, xx);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: