马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。2)FlowcontrolandexceptionHandling
Objective1)
Writecodeusingifandswitchstatementsandidentifylegalargumenttypesforthesestatements.
・Unreachablestatementsproduceacompile-timeerror.
while(false){x=3;}//won’tcompile
for(;false;){x=3;}//won’tcompile
if(false){x=3;}//willcompile,toprovidetheabilitytoconditionallycompilethecode.
while(1==2){…}//fine
・Alocalvariableinablockmaybere-declaredinanotherlocalblock,iftheblocksaredisjoint.
・iftakesabooleanarguments.Parenthesisrequired.elsepartisoptional.elseifstructureprovidesmultipleselectivebranching.
・switchtakesanargumentofbyte,short,charorint.(assignmentcompatibletoint)
・casevalueshouldbeaconstantexpressionthatcanbeevaluatedatcompiletime.
・Compilercheckseachcasevalueagainsttherangeoftheswitchexpression’sdatatype.Thefollowingcodewon’tcompile.
byteb;
switch(b){
case200://200notinrangeofbyte
default:
}
・Weneedtoplaceabreakstatementineachcaseblocktopreventtheexecutiontofallthroughothercaseblocks.Butthisisnotapartofswitchstatementandnotenforcedbythecompiler.
・defaultcasecanbeplacedanywhere.It’llbeexecutedonlyifnoneofthecasevaluesmatch.
・switchcanbenested.Nestedcaselabelsareindependent,don’tclashwithoutercaselabels.
・Emptyswitchconstructisavalidconstruct.Butanystatementwithintheswitchblockshouldcomeunderacaselabelorthedefaultcaselabel.
Objective2)
Writecodeusingallformsofloopsincludinglabeledandunlabeleduseofbreakandcontinueandstatethevaluestakenbyloopcountervariablesduringandafterloopexecution.
・3constructsCfor,while,do
・Allloopsarecontrolledbyabooleanexpression.
・Inwhileandfor,thetestoccursatthetop.Indo,testoccursatthebottom,sothebodyisexecutedatleastonce.
・Infor,wecandeclaremultiplevariablesinthefirstpartoftheloopseparatedbycommas,alsowecanhavemultiplestatementsinthethirdpartseparatedbycommas.
・Inthefirstsectionofforstatement,wecanhavealistofdeclarationstatementsoralistofexpressionstatements,butnotboth.Wecannotmixthem.
・Allexpressionsinthethirdsectionofforstatementwillalwaysexecute,evenifthefirstexpressionmakestheloopconditionfalse.ThereisnoshortCcircuithere.
・breakstatementcanbeusedwithanykindoflooporaswitchstatementorjustalabeledblock.
・continuestatementcanbeusedwithonlyaloop(anykindofloop).
・Loopscanhavelabels.Thebreakstatementabandonsprocessingofthecurrentloopentirely,thecontinuestatementonlyabandonsthecurrentlyprocessingtimearoundtheloop.
・Namesofthelabelsfollowthesamerulesasthenameofthevariables.(Identifiers)
・Labelscanhavethesamename,aslongastheydon’tencloseoneanother.
・Thereisnorestrictionagainstusingthesameidentifierasalabelandasthenameofapackage,class,interface,method,field,parameter,orlocalvariable.
・Infact,labelsmaybeappliedtoanystatements,buttheyareonlyusefulinthecontextofbeakandcontinueinloopconstructions.
Objective3)
Writecodethatmakesproperuseofexceptionsandexceptionhandlingclauses(trycatchfinally)anddeclaresmethodsandoverridingmethodsthatthrowexceptions.
・Inanymethodorconstructor(notclass)thatcontainslinesthatmightthrowacheckedexception,youmusteitherhandletheexceptionusingatry/catchconstruct,ordeclarethatthemethodthrowstheexception.Donotneedboth,butcompilerwouldn’tcompainthat.
・Java.lang.RuntimeExceptionandjava.lang.Errorneednotbehandledordeclared.
・UsethrownewxxxException()tothrowanexceptionexplicitly.Ifthethrownobjectisnull,aNullPointerExceptionwillbethrownatthehandler.
・IfamethodA()isdeclaredasthrowinganExceptionbythethrowsclause,whenyoucallA()inmethodB(),methodB()hastoeitherthrowsexceptionorcatchtheexceptionbyA().
・Anexceptioncausesajumptotheendoftryblock.Iftheexceptionoccurredinamethodcalledfromatryblock,thecalledmethodisabandoned.
・Ifthere’sacatchblockfortheoccurredexceptionoraparentclassoftheexception,theexceptionisnowconsideredhandled.
・Thetry/catchclausemusttraperrorsintheordertheirnaturalorderofhierarchy.
・Atleastone‘catch’blockorone‘finally’blockmustaccompanya‘try’statement.Ifall3blocksarepresent,theorderisimportant.(try/catch/finally)
・finallyandcatchcancomeonlywithtry,theycannotappearontheirown.
・Regardlessofwhetherornotanexceptionoccurredorwhetherornotitwashandled,ifthereisafinallyblock,it’llbeexecutedalways.(Evenifthereisareturnstatementintryblock).
・System.exit()anderrorconditionsaretheonlyexceptionswherefinallyblockisnotexecuted.
・Iftherewasnoexceptionortheexceptionwashandled,executioncontinuesatthestatementafterthetry/catch/finallyblocks.
・Iftheexceptionisnothandled,theprocessrepeatslookingfornextenclosingtryblockupthecallhierarchy.Ifthissearchreachesthetoplevelofthehierarchy(thepointatwhichthethreadwascreated),thenthethreadiskilledandmessagestacktraceisdumpedtoSystem.err.
・Ifanexceptionhandlerre-throwsanexception(throwinacatchblock),samerulesapply.Eitheryouneedtohaveatry/catchwithinthecatchorspecifytheentiremethodasthrowingtheexceptionthat’sbeingre-throwninthecatchblock.CatchblocksatthesamelevelwillnothandletheexceptionsthrowninacatchblockCitneedsitsownhandlers.
・Ifthere’snocodeintryblockthatmaythrowexceptionsspecifiedinthecatchblocks,compilerwillproduceanerror.(Thisisnotthecaseforsuper-classException)
・Inotherwords,anoverridingmethodmaynotthrowcheckedexceptionsthatarenotthrownbytheoverriddenmethod.AmethodcanreturnanException(?)
Hereistheexceptionhierarchy.
Object
|
|
Throwable
||
||
||
|Error
|
|
Exception-->ClassNotFoundException,
ClassNotSupportedException,
IllegalAccessException,
InstantiationException,
InterruptedException,
NoSuchMethodException,
RuntimeException,-----àEmptyStackException,
AWTException,NoSuchElementException,
IOExceptionArithmeticException,
ArrayStoreException,
ClassCastException,
IllegalArgumentException,---àIllegalThreadStateException
IllegalMonitorStateException,NumberFormatException
IndexOutOfBoundsException,
NegativeArraySizeException,
NullPointerException,
SecurityException.
IndexOutOfBoundsException-->ArrayIndexOutOfBoundsException,StringIndexOutOfBoundsException
IOException-->EOFException,
FileNotFoundException,
InterruptedIOException,
UTFDataFormatException,
MalformedURLException,
ProtocolException,
SockException,
UnknownHostException,
UnknownServiceException.
你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。 |