資源描述:
《java枚舉(java枚舉)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、java枚舉(java枚舉)TalkaboutusingenumerationsinJavaProgrammersmovingfromC++toJavaalwayshavealotofcomplaintsaboutJavaatfirst,withnoenumerationasamoreprominentproblem.Sowhydoesn'tJavasupportenumerations?Whatdoesitmeantosupportenumerationfromthepointofviewofprocedurallanguage?Canwefindawaytomeetther
2、equirementsofC++programmersforenumerations?Well,nowlet'sdiscusstheproblem.Enumerationtype(Enumerated,Types)Let'stakealookatthefollowingsmallprogram:Enum,Day,{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};Thisstatementprovidesauserfriendlymethodofvariabledefinitionthatenumeratesal
3、lpossiblevaluesofthisdatatype,i.e.,MondaythroughSunday.Asidefromthespecificprogramminglanguage,thecorefunctionalityofenumerationsshouldbe:Thetypeofsecurity(TypeSafety)Thecompactandeffectiveenumerationdefinition(Compact,EfficientDeclarationnumericalofEnumeratedValues)Theseamlessandotherpartso
4、ftheprogram(Seamlessintegrationwithotherinteractionlanguagefeatures)Theoperationofthehighefficiency(Runtimeefficiency)Nowlet'sdiscusseachofthesecharacteristicsonebyone.1.typesofsecurityTheenumerationdeclarationcreatesanewtype.Itisdifferentfromotherexistingtypes,includingprimitivetypes(intege
5、rs,floats,etc.)andotherenumeratedtypeswithinthescopeofthecurrentscope(Scope).Whenyouassignvaluestofunctions,integertypesandenumeratedtypesarenotinterchangeable(unlessyoudoexplicittypeconversions),thecompilerwillenforcethat.Forexample,definesuchafunctionwithanenumerationspecifiedabove:Publicv
6、oidfoo(Day);Ifyoucallthisfunctionwithintegers,thecompilerwillgiveyouthewrongone.Foo(4);//compilationerrorIfyoufollowthisstandard,thenPascal,Ada,andC++arestrictlysupportedenumerations,andtheClanguageisnot.2.compact,efficientenumerationnumericdefinitionTheprocedurefordefiningenumerationsshould
7、besimple.Forexample,inJava,wehavesuchadefinitionof"quasienumeration":PublicstaticfinalintSUNDAYpublicstaticfinalint=0;MONDAY=1;publicstaticfinalintTUESDAYpublicstaticfinalint=2;WEDNESDAY=3;publicstaticfinalintTHURSDAYpublicstaticfinalint=4;FRIDAY=5