继承机制的实现:实例

1、类的定义采用构造函数-原型方式
定义polygon类及其子类-采用混合继承方式,即用对象冒充继承构造函数的属性,用原型链继承prototype对象的方法。

function Polygon(iSides){
this.sides=iSides;
}
Polygon.prototype.getArea=function(){
return 0;
};
//三角形子类
function Triangle(iSides,iBase,iHeight){
Polygon.call(this,iSides);//用对象冒充继承构造函数的属性
this.base=iBase;
this.height=iHeight;
}
Triangle.prototype=new Polygon();//用原型链继承prototype对象的方法
Triangle.prototype.getArea=function(){
return 0.5*(this.base)*(this.height);
};
var oTriangle=new Triangle(3,4,8);
alert(oTriangle.getArea());//16
//矩形子类
function Rectangle(iLength,iWidth){
Polygon.call(this,4);//用对象冒充继承构造函数的属性
this.length=iLength;
this.width=iWidth;
}
Rectangle.prototype=new Polygon();;//用原型链继承prototype对象的方法
Rectangle.prototype.getArea=function(){
return (this.length*this.width);
}
var oRectangle=new Rectangle(4,8);
alert(oRectangle.getArea());//32

2、类的定义采用动态原型方式-继承无法利用动态原型方式实现

function Polygon(iSides){
this.sides=iSides;
if(typeof Polygon._initialized_=="undefined"){
Polygon.prototype.getArea=function(){
return 0;
}
}
Polygon._initialized_=true;
}
function Triangle(iLength,iWidth){
Polygon.call(this,3);
this.length=iLength;
this.width=iWidth;
if(typeof Triangle._initialized_=="undefined"){
Triangle.prototype=new Polygon();
Triangle.prototype.getArea=function(){
return (this.length)*(this.width);
}
}
Triangle._initialized_=true;
}
var oTriangle=new Triangle(4,8);
//控制台输出错误消息"oTriangle.getArea is not a function"
alert(oTriangle.getArea());

继承无法利用动态原型方式实现,原因在于prototype的特性,问题主要是由下面代码的位置因此的:[color=red]Triangle.prototype=new Polygon();[/color] 因为在代码运行前,对象已被实例化,并与原始的prototype对象联系在了一起。虽然用极晚绑定可使对原型对象的修改正确地表现出来,但替换 prototype对象却不会对该对象产生任何影响。只有未来的实例才会反映出这种改变,所以上面的例子会出现错误。代码进行下面的修改以后,就可以正常运行了。

function Polygon(iSides){
this.sides=iSides;
if(typeof Polygon._initialized_=="undefined"){
Polygon.prototype.getArea=function(){
return 0;
}
}
Polygon._initialized_=true;
}
function Triangle(iLength,iWidth){
Polygon.call(this,3);
this.length=iLength;
this.width=iWidth;
if(typeof Triangle._initialized_=="undefined"){
Triangle.prototype.getArea=function(){
return 0.5*(this.length)*(this.width);
}
}
Triangle._initialized_=true;
}
Triangle.prototype=new Polygon();
var oTriangle=new Triangle(4,8);
alert(oTriangle.getArea());//16


3、其他的继承方式--zInherit
(1)利用inheritfrom重写Polygon类

function Polygon(iSides){
this.sides=iSides;
}
Polygon.prototype.getArea=function(){
return 0;
}
function Triangle(iBase,iHeight){
Polygon.call(this,3);
this.base=iBase;
this.height=iHeight;
}
Triangle.prototype.inheritFrom(Polygon);
Triangle.prototype.getArea=function(){
return 0.5*(this.base)*(this.height);
}
function Rectangle(iLength,iWidth){
Polygon.call(this,4);
this.length=iLength;
this.width=iWidth;
}
Rectangle.prototype.inheritFrom(Polygon);
Rectangle.prototype.getArea=function(){
return (this.length)*(this.width);
}
var oTri=new Triangle(4,8);//16
alert(oTri.getArea());
var oRec=new Rectangle(4,8);//32
alert(oRec.getArea());
alert(oTri.instanceOf(Polygon));//true
alert(oTri.instanceOf(Triangle));//true
alert(oRec.instanceOf(Polygon));//true
alert(oRec.instanceOf(Rectangle));//true

(2)动态原型支持
动态原型支持:原型链方式不能真正符合动态原型的主旨,即把类的所有代码放置在它的构造函数中。这种方法实现的原因是,使用inheritForm()方法时,并未重写prototype对象,只是为其加入方法而已。使用这种方法,即可避开原型链的限制,实现动态原型本意。

//dinamic prototype support
function Polygon(iSides){
this.sides=iSides;
if(typeof Polygon._initialized_=="undefined"){
Polygon.prototype.getArea=function(){
return 0;
}
}
return Polygon._initialized_=true;
}
function Triangle(iBase,iHeight){
Polygon.call(this,3);
this.base=iBase;
this.height=iHeight;
if(typeof Triangle._initialized_=="undefined"){
Triangle.prototype.inheritFrom(Polygon);
Triangle.prototype.getArea=function(){
return 0.5*(this.base)*(this.height);
}
}
Triangle._initialized_=true;
}
function Rectangle(iLength,iWidth){
Polygon.call(this,4);
this.length=iLength;
this.width=iWidth;
if(typeof Rectangle._initialized_=="undefined"){
Rectangle.prototype.inheritFrom(Polygon);
Rectangle.prototype.getArea=function(){
return (this.length)*(this.width);
}
}
Rectangle._initialized_=true;
}
var tri=new Triangle(4,8);
alert(tri.getArea());//16
alert(tri.instanceOf(Polygon));//true
alert(tri.instanceOf(Triangle));//true
var rec=new Rectangle(4,8);
alert(rec.getArea());//32
alert(rec.instanceOf(Polygon));//true
alert(rec.instanceOf(Rectangle));//true

(3)多重继承支持
多重继承支持:zInherit库最有用的特性之一是支持多重继承,原型链不支持多重继承。同样利用inheritForm()方法实现。要继承属性和方法,inheritForm()方法必须与对象冒充一起使用,注意这里使用的是apply()方法。一般来说,按照继承属性的顺序继承方法比较好。

//multiple inheritance support
function ClassA(){
this.messageA="the message of Class A";
if(typeof ClassA._initialized_=="undefined"){
ClassA.prototype.showMessageA=function(){
alert(this.messageA);
};
ClassA._initialized_=true;
}
}
function ClassB(){
this.messageB="the message of Class B";
if(typeof ClassB._initialized_=="undefined"){
ClassB.prototype.showMessageB=function(){
alert(this.messageB);
};
ClassB._initialized_=true;
}
}
function ClassC(){
ClassA.apply(this);
ClassB.apply(this);
this.messageC="the message of Class C";
if(typeof ClassC._initialized_=="undefined"){
ClassC.prototype.inheritFrom(ClassA);
ClassC.prototype.inheritFrom(ClassB);
ClassC.prototype.showMessageC=function(){
alert(this.messageC);
};
ClassC._initialized_=true;
}
}
var cClass=new ClassC();
cClass.showMessageA();//the message of Class A
cClass.showMessageB();//the message of Class B
cClass.showMessageC();//the message of Class C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值