仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1265|回复: 18
打印 上一主题 下一主题

[学习教程] JAVA网页设计在Java中怎样摹拟多承继

[复制链接]
第二个灵魂 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:41:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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是必须加以改进的。
透明 该用户已被删除
沙发
发表于 2015-1-21 12:52:27 | 只看该作者
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
灵魂腐蚀 该用户已被删除
板凳
发表于 2015-1-24 14:29:46 | 只看该作者
Java 编程语言的风格十分接近C、C++语言。
柔情似水 该用户已被删除
地板
发表于 2015-2-1 16:51:01 | 只看该作者
Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台
小妖女 该用户已被删除
5#
发表于 2015-2-7 09:49:16 | 只看该作者
Java 编程语言的风格十分接近C、C++语言。
深爱那片海 该用户已被删除
6#
发表于 2015-2-7 11:04:50 | 只看该作者
另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。
小女巫 该用户已被删除
7#
发表于 2015-2-21 19:17:29 | 只看该作者
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
金色的骷髅 该用户已被删除
8#
发表于 2015-2-26 20:24:28 | 只看该作者
你就该学一学Servlet了。Servlet就是服务器端小程序,他负责生成发送给客户端的HTML文件。JSP在执行时,也是先转换成Servlet再运行的。虽说JSP理论上可以完全取代Servlet,这也是SUN推出JSP的本意,可是Servlet用来控制流程跳转还是挺方便的,也令程序更清晰。接下来你应该学习一下Javabean了,可能你早就看不管JSP在HTML中嵌Java代码的混乱方式了,这种方式跟ASP又有什么区别呢?
谁可相欹 该用户已被删除
9#
发表于 2015-2-27 22:51:54 | 只看该作者
所以现在应用最广泛又最好学的就是J2EE了。 J2EE又包括许多组件,如Jsp,Servlet,JavaBean,EJB,JDBC,JavaMail等。要学习起来可不是一两天的事。那么又该如何学习J2EE呢?当然Java语法得先看一看的,I/O包,Util包,Lang包你都熟悉了吗?然后再从JSP学起。
莫相离 该用户已被删除
10#
发表于 2015-3-9 14:58:04 | 只看该作者
学Java必读的两个开源程序就是Jive和Pet Store.。 Jive是国外一个非常著名的BBS程序,完全开放源码。论坛的设计采用了很多先进的技术,如Cache、用户认证、Filter、XML等,而且论坛完全屏蔽了对数据库的访问,可以很轻易的在不同数据库中移植。论坛还有方便的安装和管理程序,这是我们平时编程时容易忽略的一部份(中国程序员一般只注重编程的技术含量,却完全不考虑用户的感受,这就是我们与国外软件的差距所在)。
海妖 该用户已被删除
11#
发表于 2015-3-10 04:53:28 | 只看该作者
在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。
不帅 该用户已被删除
12#
发表于 2015-3-15 15:24:02 | 只看该作者
Pet Store.(宠物店)是SUN公司为了演示其J2EE编程规范而推出的开放源码的程序,应该很具有权威性,想学J2EE和EJB的朋友不要 错过了。
爱飞 该用户已被删除
13#
发表于 2015-3-16 08:16:36 | 只看该作者
你快去找一份Java的编程工作来做吧(如果是在校学生可以去做兼职啊),在实践中提高自己,那才是最快的。不过你得祈祷在公司里碰到一个高手,而且他 还愿意不厌其烦地教你,这样好象有点难哦!还有一个办法就是读开放源码的程序了。我们知道开放源码大都出自高手,他们设计合理,考虑周到,再加上有广大的程序员参与,代码的价值自然是字字珠叽,铿锵有力(对不起,偶最近《金装四大才子》看多了)。
小魔女 该用户已被删除
14#
发表于 2015-3-22 01:10:38 | 只看该作者
Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)
冷月葬花魂 该用户已被删除
15#
发表于 2015-3-28 04:19:43 | 只看该作者
Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)
活着的死人 该用户已被删除
16#
发表于 2015-3-31 23:07:28 | 只看该作者
是一种语言,用以产生「小应用程序(Applet(s))
乐观 该用户已被删除
17#
发表于 2015-4-15 06:52:47 | 只看该作者
一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从
第二个灵魂 该用户已被删除
18#
 楼主| 发表于 2015-5-6 23:10:14 | 只看该作者
那么我书也看了,程序也做了,别人问我的问题我都能解决了,是不是就成为高手了呢?当然没那么简单,这只是万里长征走完了第一步。不信?那你出去接一个项目,你知道怎么下手吗,你知道怎么设计吗,你知道怎么组织人员进行开发吗?你现在脑子里除了一些散乱的代码之外,可能再没有别的东西了吧!
愤怒的大鸟 该用户已被删除
19#
发表于 2015-6-6 15:56:34 | 只看该作者
科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-5 18:00

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表