|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
微软什么都提供了。你可以试想一下,如果你是新手,你是希望你点一下按钮程序就能运行那,还是想自己一点一点的组织结构,然后打包发部,调错再打包......承继SimulatingMultipleInheritanceinJava
ArticleAuthor:MikeVanAtter
FromBook:CodeNotesforJava
DatePublished:February1,2002
PurposeofMultipleInheritance
Multipleinheritanceallowsasingleclasstoextendtwoparentclassesandthusprovidethemethodsofbothparentclasses.UnlikeC++,Javadoesnotexplicitlysupportmultipleinheritance,allowingaclasstoextendonlyasingleparentclass.However,aswewillshowinthisarticle,itispossibletosimulatemultipleinheritance,allowingasingleclasstoprovidethemethods,andtherespectiveimplementations,oftwoparentclasses.Thestrategythatisintroducedinthisarticleisalsoeasilyextendibletoprovideinheritanceofthreeormoreparentclasses.
SimulatingMultipleInheritance
InthisarticlewewilluseasimpleexampletodemonstratehowtosimulatemultipleinheritanceinJava.WewillbeginwiththeNextOddandNextEvenclasses,showninListing1.1andListing1.2respectively.Wewillthencreateanewclass,whichwewillcallEvenOdd,thatprovidesthefunctionalityofbothclasses.
//RepeatedcallstothegetNextOddmethodwill
returnthenext
//oddnumber(i.e.thefirstcallwillreturn1,thesecond
//call3,etc.
publicclassNextOdd{
//thelastoddnumberreturnedbythegetNextOddmethod
privateintlastOdd=-1;
publicNextOdd(){
this.lastOdd=-1;
}//NextOdd
//selectsadifferentstartingpointfortheoddnumbers
//ensuresthatthestartingpointisinfactanoddnumber
publicNextOdd(intstart){
this.lastOdd=((int)start/2)*2+1;
}//NextOdd
//Retrievesthenextoddnumber
publicintgetNextOdd(){
returnlastOdd+=2;
}//getNext
}//NextOdd
Listing1.1:NextOdd.java
//RepeatedcallstothegetNextEvenmethodwill
returnthe
//nextevennumber(i.ethefirstcallwillreturn0,the
//secondcallwillreturn2,etc.)
publicclassNextEven{
//thelastevennumberreturnedbygetNextEven
privateintlastEven=-2;
publicNextEven(){
this.lastEven=-2;
}//NextEven
//selectsadifferentstartingpointfortheevennumbers
//ensuresthatthestartingpointisinfactaneven#
publicNextEven(intstart){
this.lastEven=((int)(start/2))*2;
}//constructor
//retrievesthenextevennumber
publicintgetNextEven(){
returnlastEven+=2;
}//getNextEven
}//NextEven
Listing1.2:NextEven.java
AsJavaonlyallowsforextendingasingleclassthroughtheextendskeyword,wewillhavetoprovideanothermannerforextendingmorethanoneclass.Inthisexample,wewillextendtheNextEvenclassbyusingtheextendskeywordanduseanewinterface,whichwewillcallOddInterface,andanimplementationofthenewinterface,whichwewillcallOddChild,toextendtheNextOddclass.
ThefirststepinextendingtheNextOddclassistodefineaninterfacewiththesamemethodsastheNextOddclass,asshowninListing1.3.Noticethattheparameters,functionnames,andreturnvaluesforallmethodsintheinterfacemustbethesameastheoriginalclass.
publicinterfaceOddInterface{
publicintgetNextOdd();
}//OddInterface
Listing1.3:OddInterface.java
OncewehavecreatedOddInterface,thenextstepistocreateanimplementationofOddInterfacethatalsoextendstheNextOddclass,asshowninListing1.4.ByextendingtheNextOddclass,which,aspreviouslyexplained,hasallthesamemethodprototypesasOddInterface,wedonothavetoimplementanyofthemethodsinOddInterfaceandonlyhavetoprovideconstructorsforthenewclass,whichwewillcallOddChild.TheseconstructorssimplycalltheconstructorsoftheNextOddclassusingthesuper()method.TheOddChildclassnowprovidestheexactimplementationofallmethodsoftheNextOddclass,withoutthedeveloperhavingtoknowanythingaboutthewayinwhichNextOddwasoriginallyimplemented.
publicclassOddChildextendsNextOddimplements
OddInterface{
publicOddChild(){
super();
}//constructor
publicOddChild(intstart){
super(start);
}//constructor
}//OddChild
Listing1.4:OddChild.java
WithourimplementationoftheOddInterfaceclass,wecannowcreateaclassthatwillextendboththeNextEvenclassandtheNextOddclass.ThisnewclasswillbecalledEvenOddandisshowninListing1.5.BecauseJavaallowsyoutoextendonlyasingleclass,EvenOddwillextendtheNextEvenclassanduseOddInterfaceandOddChildtoextendtheNextOddclass.
InordertobeabletocalltheEvenOdd.getNextOdd()method,EvenOddwillimplementOddInterfacebecauseOddInterfacehasallthesamemethodprototypesasNextOdd.ThismeansthatwealsomustprovideanimplementationofalltheOddInterfacemethods,andasaresultalltheNextOddmethods,withinEvenOdd.ToensurethesemethodshavethesameimplementationastheNextOddmethods,wewillcreateaprivateinstanceoftheOddChildclass,whichwewillcalloddGenerator,andcalltherespectiveoddGeneratormethod.Forexample,intheEvenOdd.getNextOdd()method,wecalloddGenerator.getNextOdd().TheEvenOddclassnowprovidesthesamefunctionalityandimplementationofboththeNextOddandNextEvenclasses.
publicclassEvenOddextendsNextEvenimplements
OddInterface{
publicEvenOdd(){
super();
oddGenerator=newOddChild();
}//EvenOdd
//initializesthestartingpointofboththeodd#generator
//andtheeven#generator
publicEvenOdd(intoddStart,intevenStart){
super(evenStart);
oddGenerator=newOddChild(oddStart);
}//EvenOdd
publicintgetNextOdd(){
returnoddGenerator.getNextOdd();
}//getNextOdd
privatefinalOddInterfaceoddGenerator;
}//EvenOdd
Listing1.5:EvenOdd.java
Unfortunately,becauseJavadoesonlyallowyoutoextendasingleclass,youwillonlybeabletocasttheEvenOddclasstoaNextEvenclassandnottoaNextOddclassasyouwouldbeabletoifmultipleinheritanceweredirectlysupportedbyJava.IfyouwishtobeabletocastanEvenOddobjecttoaNextOddclass,youwillhavetoprovideamethodforextractinganinstanceoftheNextOddclasssimilartothegetNextOddObj()methodinListing1.6.
publicNextOddgetNextOddObj(){
return(NextOdd)oddGenerator;
}//getNextOdd
Listing1.6:ReturningaNextOddinstance
Infact,thismultipleinheritancelimitationisoftenavoidedbycreatingafactoryclasswithmanymethodssimilartoListing1.6.
Summary
Createaninterfacewithallthesamemethodprototypesasthebaseclassyouwillbeextending.
Createaclassthatimplementstheinterfacecreatedinstep1andextendsthebaseclass.
Inthechildclass,implementtheinterfacecreatedinstep1andcreateaprivateinstanceoftheclassdefinedinstep2.Inallthemethodsdefinedintheinterface,simplycallthecorrespondingmethodintheclasscreatedinstep2.
从一个编程语言的普及程度来将,一个好的IDE是至关中要的,而现在的java的IDE虽然已经很好了,但是和.net比起来还是稍微差一些的,这是个客观事实。java要想普及的更好。DE是必须加以改进的。 |
|