主题:[原创]关于JS中继承用法的最新研究
网上有一些关于JS继承用法,最常用的如下:
function cPerson(name){
this.Name=name;
this.GetName=function(){
alert(this.Name)
}
function cStudent(name){
this.pClass=cPerson;
this.pClass(name);
}
这种继承可以继承父类的所有属性和方法,但如果方法需要部分重写,就不行了,因为一旦在子类中定义了父类的方法GetName,则父类的GetName方法的所有功能代码全部被重写了。
经过研究,我发现可以对父类方法继续进行继承,通过这样的方法,也可以进行类的多重往下继承,代码如下,供大家研究:
//父类:cPerson----------------------------------------
function cPerson(name){
this.Name=name;
}
cPerson.prototype.GetName=function(){
//注:为了让类可以一层层的继承下去,并且对类的方法也可以继承,
//则类的方法必须用此方法编写,不能写有cPerson类中。
alert(this.Name)
}
//子类:cPerson.cStudent-------------------------------
function cStudent(name){
//对父类进行继承,因为父类的function内部只有属性,所以此处只继承了属性。
this.pClass=cPerson();
this.pClass(name);
}
cStudent.prototype.GetName=function(){//编写cStudent类的GetName方法。
//以下两句继承父类的GetName方法
//如果要在父类GetName的基础上有自己的GetName方法,可以在this.pGetName的前面或后面添加自己的语句。
this.pGetName=cPerson.prototype.GetName;
//注:此处的cPerson不能写成this.pClass,否则不能继续往下继承
//this.Name="cStudent"; //可以在继承父类方法的基础上有子类自己的语句
this.pGetName();
alert("cStudent");
}
//子类的子类:cPerson.cStudent.cStudentGroup--------------
function cStudentGroup(){
this.pClass=cStudent();
this.pClass(name);
}
cStudentGroup.prototype.GetName=function(){//编写cStudentGroup类的GetName方法。
this.pGetName=cStudent.prototype.GetName;
//this.Name="cStudentGroup"; //可以在继承父类方法的基础上有子类自己的语句
this.pGetName();
alert("cStudentGroup");
}
//使用举例:---------------------------------------------
var Man=new cStudentGroup("xxx");
Man.GetName();
//结果说明:---------------------------------------------
1、建立Man实例时,根据代码会逐步追溯到最上层的父类cPerson,并运行其cPerson.GetName方法,因此会调用:alert(this.Name),即:alert("xxx");
2、然后调用到第二层的扩展代码,即:cStudent.GetName中的alert("cStudent");
3、最后调用cStudentGroup.GetName方法中的扩展代码:alert("cStudentGroup");
function cPerson(name){
this.Name=name;
this.GetName=function(){
alert(this.Name)
}
function cStudent(name){
this.pClass=cPerson;
this.pClass(name);
}
这种继承可以继承父类的所有属性和方法,但如果方法需要部分重写,就不行了,因为一旦在子类中定义了父类的方法GetName,则父类的GetName方法的所有功能代码全部被重写了。
经过研究,我发现可以对父类方法继续进行继承,通过这样的方法,也可以进行类的多重往下继承,代码如下,供大家研究:
//父类:cPerson----------------------------------------
function cPerson(name){
this.Name=name;
}
cPerson.prototype.GetName=function(){
//注:为了让类可以一层层的继承下去,并且对类的方法也可以继承,
//则类的方法必须用此方法编写,不能写有cPerson类中。
alert(this.Name)
}
//子类:cPerson.cStudent-------------------------------
function cStudent(name){
//对父类进行继承,因为父类的function内部只有属性,所以此处只继承了属性。
this.pClass=cPerson();
this.pClass(name);
}
cStudent.prototype.GetName=function(){//编写cStudent类的GetName方法。
//以下两句继承父类的GetName方法
//如果要在父类GetName的基础上有自己的GetName方法,可以在this.pGetName的前面或后面添加自己的语句。
this.pGetName=cPerson.prototype.GetName;
//注:此处的cPerson不能写成this.pClass,否则不能继续往下继承
//this.Name="cStudent"; //可以在继承父类方法的基础上有子类自己的语句
this.pGetName();
alert("cStudent");
}
//子类的子类:cPerson.cStudent.cStudentGroup--------------
function cStudentGroup(){
this.pClass=cStudent();
this.pClass(name);
}
cStudentGroup.prototype.GetName=function(){//编写cStudentGroup类的GetName方法。
this.pGetName=cStudent.prototype.GetName;
//this.Name="cStudentGroup"; //可以在继承父类方法的基础上有子类自己的语句
this.pGetName();
alert("cStudentGroup");
}
//使用举例:---------------------------------------------
var Man=new cStudentGroup("xxx");
Man.GetName();
//结果说明:---------------------------------------------
1、建立Man实例时,根据代码会逐步追溯到最上层的父类cPerson,并运行其cPerson.GetName方法,因此会调用:alert(this.Name),即:alert("xxx");
2、然后调用到第二层的扩展代码,即:cStudent.GetName中的alert("cStudent");
3、最后调用cStudentGroup.GetName方法中的扩展代码:alert("cStudentGroup");

您所在位置: