小魔女 发表于 2015-1-18 11:55:15

JAVA网站制作之Overloading overriding runtime type ...

还得说上一点,就java本质而言,是面相对象的,但是你有没有发现,java也不全是,比如说基本类型,int,那他就是整型而不是对象,转换类型是还得借助包装类。loading|objectObjective3)
Writecodetoconstructinstancesofanyconcreteclassincludingnormaltoplevelclassesinnerclassesstaticinnerclassesandanonymousinnerclasses.
InnerClasses
・Aclasscanbedeclaredinanyscope.Classesdefinedinsideofotherclassesareknownasnestedclasses.Therearefourcategoriesofnestedclasses.
Top-levelnestedclasses/interfaces
・Declaredasaclassmemberwithstaticmodifier.
・Justlikeotherstaticfeaturesofaclass.Canbeaccessed/instantiatedwithoutaninstanceoftheouterclass.Canaccessonlystaticmembersofouterclass.Can’taccessnon-staticinstancevariablesormethods.
・Verymuchlikeany-otherpackagelevelclass/interface.Provideanextensiontopackagingbythemodifiednamingschemeatthetoplevel.
・Classescandeclarebothstaticandnon-staticmembers.
・Anyaccessibilitymodifiercanbespecified.
・Nestedinterfacesareimplicitlystatic(staticmodifieralsocanbespecified).Theycanhaveanyaccessibilitymodifier.Therearenonon-staticinner,localoranonymousinterfaces.
Non-staticinnerclasses
・Declaredasaclassmemberwithoutstatic.
・Aninstanceofanon-staticinnerclasscanexistonlywithaninstanceofitsenclosingclass.Soitalwayshastobecreatedwithinacontextofanouterinstance.
Outer.Inneri=newOuter().newInner();
・Justlikeothernon-staticfeaturesofaclass.Canaccessallthefeatures(evenprivate)oftheenclosingouterclass.Haveanimplicitreferencetotheenclosinginstance.
・Cannothaveanystaticmembers.
・Canhaveanyaccessmodifier.
Localclasses
・Definedinsideablock(couldbeamethod,aconstructor,alocalblock,astaticinitializeroraninstanceinitializer).Cannotbespecifiedwithstaticmodifier.
・Cannothaveanyaccessmodifier(sincetheyareeffectivelylocaltotheblock)
・Cannotdeclareanystaticmembers.(Evendeclaredinastaticcontext)
・Canaccessallthefeaturesoftheenclosingclass(becausetheyaredefinedinsidethemethodoftheclass)butcanaccessonlyfinalvariablesdefinedinsidethemethod(includingmethodarguments).Thisisbecausetheclasscanoutlivethemethod,butthemethodlocalvariableswillgooutofscopeCincaseoffinalvariables,compilermakesacopyofthosevariablestobeusedbytheclass.(Newmeaningforfinal)
・Sincethenamesoflocalclassesarenotvisibleoutsidethelocalcontext,referencesoftheseclassescannotbedeclaredoutside.Sotheirfunctionalitycouldbeaccessedonlyviasuper-classreferences(eitherinterfacesorclasses).Objectsofthoseclasstypesarecreatedinsidemethodsandreturnedassuper-classtypereferencestotheoutsideworld.Thisisthereasonthattheycanonlyaccessfinalvariableswithinthelocalblock.Thatway,thevalueofthevariablecanbealwaysmadeavailabletotheobjectsreturnedfromthelocalcontexttooutsideworld.
・Cannotbespecifiedwithstaticmodifier.Butiftheyaredeclaredinsideastaticcontextsuchasastaticmethodorastaticinitializer,theybecomestaticclasses.Theycanonlyaccessstaticmembersoftheenclosingclassandlocalfinalvariables.Butthisdoesn’tmeantheycannotaccessanynon-staticfeaturesinheritedfromsuperclasses.Thesefeaturesaretheirown,obtainedviatheinheritancehierarchy.Theycanbeaccessednormallywith‘this’or‘super’.
Anonymousclasses
・Anonymousclassesaredefinedwheretheyareconstructed.Theycanbecreatedwhereverareferenceexpressioncanbeused.
・Ananonymousclassisneverabstract.Ananonymousclassisalwaysaninnerclass;itisneverstatic.Ananonymousclassisalwaysimplicitlyfinal.
・Anonymousclassescannothaveexplicitconstructors.Instanceinitializerscanbeusedtoachievethefunctionalityofaconstructor.
・Anonymousclassescanimplementaninterface(implicitextensionofObject)orexplicitlyextendaclass.Cannotdoboth.
Syntax:newinterfacename(){}ornewclassname(cantakearg.){}
・Keywordsimplementsandextendsarenotusedinanonymousclasses.
・Abstractclassescanbespecifiedinthecreationofananonymousclass.Thenewclassisaconcreteclass,whichautomaticallyextendstheabstractclass.
・Discussionforlocalclassesonstatic/non-staticcontext,accessingenclosingvariables,anddeclaringstaticvariablesalsoholdsgoodforanonymousclasses.Inotherwords,anonymousclassescannotbespecifiedwithstatic,butbasedonthecontext,theycouldbecomestaticclasses.Inanycase,anonymousclassesarenotallowedtodeclarestaticmembers.Basedonthecontext,non-static/staticfeaturesofouterclassesareavailabletoanonymousclasses.Localfinalvariablesarealwaysavailabletothem.
・E.g.
btn.addActionListener(
newActionListener(){
voidActionPerformed(){System.out.println(s);}
}
)


・Oneenclosingclasscanhavemultipleinstancesofinnerclasses.
・Innerclassescanhavesynchronousmethods.Butcallingthosemethodsobtainsthelockforinnerobjectonly,nottheouterobject.Ifyouneedtosynchronizeaninnerclassmethodbasedonouterobject,outerobjectlockmustbeobtainedexplicitly.Locksoninnerobjectandouterobjectareindependent.
・Nestedclassescanextendanyclassorcanimplementanyinterface.Norestrictions.
・Allnestedclasses(exceptanonymousclasses)canbeabstractorfinal.
・Classescanbenestedtoanydepth.Top-levelstaticclassescanbenestedonlywithinotherstatictop-levelclassesorinterfaces.Deeplynestedclassesalsohaveaccesstoallvariablesoftheouter-mostenclosingclass(aswelltheimmediateenclosingclass’s)
・Memberinnerclassescanbeforwardreferenced.Localinnerclassescannotbe.(?)
・Outerclassvariablesareaccessiblewithintheinnerclass,buttheyarenotinherited.Theydon’tbecomemembersoftheinnerclass.Thisisdifferentfrominheritance.(Outerclasscannotbereferredusing‘super’,andouterclassvariablescannotbeaccessedusing‘this’)
・Aninnerclassvariablecanshadowanouterclassvariable.Iftheinnerclassissub-classedwithinthesameouterclass,thevariablehastobequalifiedexplicitlyinthesub-class.Tofullyqualifythevariable,use(classname.this.variablename).Ifwedon’tcorrectlyqualifythevariable,acompilererrorwilloccur.(Notethatthisdoesnothappeninmultiplelevelsofinheritancewhereanupper-mostsuper-class’svariableissilentlyshadowedbythemostrecentsuper-classvariableorinmultiplelevelsofnestedinnerclasseswhereaninner-mostclass’svariablesilentlyshadowsanouter-mostclass’svariable.Problemcomesonlywhenthesetwohierarchychains(inheritanceandcontainment)clash.)
・Iftheinnerclassissub-classedoutsideoftheouterclass(onlypossiblewithtop-levelnestedclasses)explicitqualificationisnotneeded(itbecomesregularclassinheritance)
・Innerclasscannothavesomenameasanyofitsenclosingclass.



EntityDeclarationContextAccessibilityModifiersOuterinstanceDirectAccesstoenclosingcontextDefinesstaticornon-staticmembers
PackagelevelclassAspackagememberPublicordefaultNoN/ABothstaticandnon-static
Toplevelnestedclass(static)AsstaticclassmemberAllNoStaticmembersinenclosingcontextBothstaticandnon-static
NonstaticinnerclassAsnon-staticclassmemberAllYesAllmembersinenclosingcontextOnlynon-static
Localclass(non-static)Inblockwithnon-staticcontextNoneYesAllmembersinenclosingcontext+localfinalvariablesOnlynon-static
Localclass(static)InblockwithstaticcontextNoneNoStaticmembersinenclosingcontext+localfinalvariablesOnlynon-static
Anonymousclass(non-static)Inblockwithnon-staticcontextNoneYesAllmembersinenclosingcontext+localfinalvariablesOnlynon-static
Anonymousclass(static)InblockwithstaticcontextNoneNoStaticmembersinenclosingcontext+localfinalvariablesOnlynon-static
PackagelevelinterfaceAspackagememberPublicordefaultNoN/AStaticvariablesandnon-staticmethodprototypes
Toplevelnestedinterface(static)AsstaticclassmemberAllNoStaticmembersinenclosingcontextStaticvariablesandnon-staticmethodprototypes


//Example1

publicclassInnerInnerTest{
publicstaticvoidmain(Strings[]){
newOuter().newInner().newInnerInner().newInnerInnerInner().doSomething();
newOuter().newInnerChild().doSomething();
newOuter2().newInner2().newInnerInner2().doSomething();
newInnerChild2().doSomething();

}
}

classOuter{
Stringname="Vel";

classInner{
Stringname="Sharmi";

classInnerInner{

classInnerInnerInner{

publicvoiddoSomething(){

//Noprobleminaccessingwithoutfullqualification,
//inner-mostclassvariableshadowstheouter-mostclassvariable
System.out.println(name);//Prints"Sharmi"
System.out.println(Outer.this.name);//Prints"Vel",explicitreferencetoOuter

//error,variableisnotinheritedfromtheouterclass,itcanbejustaccessible
//System.out.println(this.name);
//System.out.println(InnerInner.this.name);
//System.out.println(InnerInnerInner.this.name);

//error,supercannotbeusedtoaccessouterclass.
//superwillalwaysrefertheparent,inthiscaseObject
//System.out.println(super.name);

System.out.println(Inner.this.name);//Prints"Sharmi",Innerhasdeclaredname
}
}
}
}

/*Thisisaninnerclassextendinganinnerclassinthesamescope*/
classInnerChildextendsInner{
publicvoiddoSomething(){
//compilererror,explicitqualifierneeded
//nameisinheritedfromInner,Outersnameisalsoinscope
//System.out.println(name);
System.out.println(Outer.this.name);//prints"Vel",explicitreferencetoOuter
System.out.println(super.name);//prints"Sharmi",Innerhasdeclaredname
System.out.println(this.name);//prints"Sharmi",nameisinheritedbyInnerChild
}
}
}

classOuter2{
staticStringname="Vel";
staticclassInner2{
staticStringname="Sharmi";

classInnerInner2{
publicvoiddoSomething(){
System.out.println(name);//prints"Sharmi",inner-mosthidesouter-most
System.out.println(Outer2.name);//prints"Vel",explicitreferencetoOuter2sstaticvariable
//System.out.println(this.name);//error,nameisnotinherited
//System.out.println(super.name);//error,superreferstoObject
}
}
}

}

/*Thisisastand-aloneclassextendinganinnerclass*/
classInnerChild2extendsOuter2.Inner2{
publicvoiddoSomething(){
System.out.println(name);//prints"Sharmi",Inner2snameisinherited
System.out.println(Outer2.name);//prints"Vel",explicitreferencetoOuter2sstaticvariable
System.out.println(super.name);//prints"Sharmi",Inner2hasdeclaredname
System.out.println(this.name);//prints"Sharmi",nameisinheritedbyInnerChild2
}
}

//Example2

publicclassInnerTest2{
publicstaticvoidmain(Strings[]){

newOuterClass().doSomething(10,20);

//Thisislegal
//OuterClass.InnerClassic=newOuterClass().newInnerClass();
//ic.doSomething();

//Compilererror,localinnerclassescannotbeaccessedfromoutside
//OuterClass.LocalInnerClasslic=newOuterClass().newLocalInnerClass();
//lic.doSomething();

newOuterClass().doAnonymous();

}
}

classOuterClass{
finalinta=100;
privateStringsecret="Nothingserious";

publicvoiddoSomething(intarg,finalintfa){
finalintx=100;
inty=200;

System.out.println(this.getClass()+"-indoSomething");
System.out.print("a="+a+"secret="+secret+"arg="+arg+"fa="+fa);
System.out.println("x="+x+"y="+y);

//Compilererror,forwardreferenceoflocalinnerclass
//newLocalInnerClass().doSomething();

abstractclassAncestorLocalInnerClass{}//innerclasscanbeabstract

finalclassLocalInnerClassextendsAncestorLocalInnerClass{//canbefinal
publicvoiddoSomething(){
System.out.println(this.getClass()+"-indoSomething");
System.out.print("a="+a);
System.out.print("secret="+secret);
//System.out.print("arg="+arg);//Compilererror,accessingnon-finalargument
System.out.print("fa="+fa);
System.out.println("x="+x);
//System.out.println("y="+y);//Compilererror,accessingnon-finalvariable
}
}

newInnerClass().doSomething();//forwardreferencefineformemberinnerclass
newLocalInnerClass().doSomething();
}

abstractclassAncestorInnerClass{}

interfaceInnerInterface{finalintsomeConstant=999;}//innerinterface

classInnerClassextendsAncestorInnerClassimplementsInnerInterface{
publicvoiddoSomething(){
System.out.println(this.getClass()+"-indoSomething");
System.out.println("a="+a+"secret="+secret+"someConstant="+someConstant);
}
}

publicvoiddoAnonymous(){
//Anonymousclassimplementingtheinnerinterface
System.out.println((newInnerInterface(){}).someConstant);

//Anonymousclassextendingtheinnerclass
(newInnerClass(){
publicvoiddoSomething(){
secret="secretischanged";
super.doSomething();
}
}).doSomething();
}
}

Thusanabstractclasscannotbeinstantiatedandsoanobjectreferencecannotbecreated.Rememberthataclassthatcontainsanyabstractmethodstheclassitselfisabstractandcannotbeinstantiated.

Alocalclassisvisibleonlywithinitscodeblockormethod.

Forclassinsideinterface:(?)
1.Theclassisalwayspublic.
2.Theclassisalwaysstatic.
3.theclassmethodscannotcallthemethodsdeclaredintheinterface.

Q1
publicclassMyClass1{
publicstaticvoidmain(Stringargv[]){}
/*ModifieratXX*/classMyInner{}
}
WhatmodifierswouldbelegalatXXintheabovecode?
1)public
2)private
3)static
4)friend


比如模式、敏捷方法什么的,这些思想好,但是实施的人没有理解而且没有正确运用这些知识导致了开发周期的延长。比如说对象,通过getName()方法不能获取对象的名字。

再见西城 发表于 2015-1-21 17:03:07

是一种使用者不需花费很多时间学习的语言

第二个灵魂 发表于 2015-1-30 21:29:24

多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。

变相怪杰 发表于 2015-2-4 01:42:30

任职于太阳微系统的詹姆斯·高斯林等人于1990年代初开发Java语言的雏形,最初被命名为Oak,目标设置在家用电器等小型系统的程序语言

小女巫 发表于 2015-2-9 09:14:09

有时间再研究一下MVC结构(把Model-View-Control分离开的设计思想)

乐观 发表于 2015-2-27 05:09:39

有时间再研究一下MVC结构(把Model-View-Control分离开的设计思想)

admin 发表于 2015-3-1 18:38:07

所以现在应用最广泛又最好学的就是J2EE了。 J2EE又包括许多组件,如Jsp,Servlet,JavaBean,EJB,JDBC,JavaMail等。要学习起来可不是一两天的事。那么又该如何学习J2EE呢?当然Java语法得先看一看的,I/O包,Util包,Lang包你都熟悉了吗?然后再从JSP学起。

飘灵儿 发表于 2015-3-6 19:32:34

J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。

小魔女 发表于 2015-3-13 06:35:31

至于JDBC,就不用我多说了,你如果用java编过存取数据库的程序,就应该很熟悉。还有,如果你要用Java编发送电子邮件的程序,你就得看看Javamail 了。

冷月葬花魂 发表于 2015-3-16 04:55:40

是一种语言,用以产生「小应用程序(Applet(s))

兰色精灵 发表于 2015-3-21 01:12:58

你可以去承接一些项目做了,一开始可能有些困难,可是你有技术积累,又考虑周全,接下项目来可以迅速作完,相信大家以后都会来找你的,所以Money就哗啦啦的。。。。。。

透明 发表于 2015-3-30 19:49:20

http://www.jdon.com/去下载,或到同济技术论坛的服务器ftp://nro.shtdu.edu.cn去下,安装上有什么问题,可以到论坛上去提问。

精灵巫婆 发表于 2015-4-10 16:34:04

不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。

海妖 发表于 2015-4-18 04:50:13

Java 不同于一般的编译执行计算机语言和解释执行计算机语言。它首先将源代码编译成二进制字节码(bytecode),然后依赖各种不同平台上的虚拟机来解释执行字节码。从而实现了“一次编译、到处执行”的跨平台特性。

山那边是海 发表于 2015-4-26 01:15:56

是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能

莫相离 发表于 2015-4-26 20:09:40

Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)

仓酷云 发表于 2015-5-1 02:10:17

不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。

金色的骷髅 发表于 2015-6-20 17:35:52

自从Sun推出Java以来,就力图使之无所不包,所以Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE,这也就是Sun ONE(Open Net Environment)体系。J2SE就是Java2的标准版,主要用于桌面应用软件的编程;J2ME主要应用于嵌入是系统开发,如手机和PDA的编程;J2EE是Java2的企业版,主要用于分布式的网络程序的开发,如电子商务网站和ERP系统。

柔情似水 发表于 2015-7-11 05:55:50

学Java必读的两个开源程序就是Jive和Pet Store.。 Jive是国外一个非常著名的BBS程序,完全开放源码。论坛的设计采用了很多先进的技术,如Cache、用户认证、Filter、XML等,而且论坛完全屏蔽了对数据库的访问,可以很轻易的在不同数据库中移植。论坛还有方便的安装和管理程序,这是我们平时编程时容易忽略的一部份(中国程序员一般只注重编程的技术含量,却完全不考虑用户的感受,这就是我们与国外软件的差距所在)。

蒙在股里 发表于 2015-7-12 04:58:57

有时间再研究一下MVC结构(把Model-View-Control分离开的设计思想)
页: [1]
查看完整版本: JAVA网站制作之Overloading overriding runtime type ...