父类继承方法、父类特有方法、子类继承方法、子类覆盖继承方法(子类特有方法)
//定义一个父类
function Complex(real , imaginary){
this.x=real;
this.y=imaginary;
}
Complex.prototype.toString=function(){
return this.x+"+"+this.y+"j";
}
Complex.magnitude=function()
{
}
//不会被继承Complex函数(这时不要加this了)
function MoreComplex(real , imaginary){
this.x=real;
this.y=imaginary;
}
MoreComplex.prototype=new Complex(0,0);
//MoreComplex.prototype.constructor=MoreComplex;
//定义子类自己的函数
MoreComplex.prototype.swap=function(){
var tmp=this.x;
this.x=this.y;
this.y=tmp;
}
a=new MoreComplex(11,33);
a.toString = function(){
return this.x*this.y;
}
A=new Complex(10,115);
A.magnitude = function(){
return (this.x + "+" + this.y);
}
alert(a);
alert(A);
a.swap(); //调用子类特有方法转换值,在应用继承方法返回值
alert(a);
构造函数继承方法的说明:
最新推荐文章于 2022-04-29 16:40:51 发布