资源描述:
《用enum(枚举类型)取代整数集(replacing integer sets with enum (enumeration type))》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、用enum(枚举类型)取代整数集(Replacingintegersetswithenum(enumerationtype))IntheAndroidofAPI,youcanfindmanyplacesthatuseintegersetsasparameters,solet'stakealookattheexamples.LinearLayoutisknownasabasicelementofUI,whichhastheattributesofadirection,whichcanbesetbythefollowingmethods:Viewplaincop
2、ytoclipboardprint?01.LinearLayout.setOrientation(int);LinearLayout.setOrientation(int);it'susuallyusedwhenit'sused:Viewplaincopytoclipboardprint?01.LinearLayout.setOrientation(LinearLayout.HORIZONTAL);02.LinearLayout.setOrientation(LinearLayout.VERTICAL);LinearLayout.setOrientation
3、(LinearLayout.HORIZONTAL);LinearLayout.setOrientation(LinearLayout.VERTICAL);butitcanalsobeusedlikethis:Viewplaincopytoclipboardprint?01.LinearLayout.setOrientation(0);//LinearLayout.HORIZONTAL=002.LinearLayout.setOrientation(1);//LinearLayout.VERTICAL=0x01LinearLayout.setOrientati
4、on(0);//LinearLayout.HORIZONTAL=0LinearLayout.setOrientation(1);//LinearLayout.VERTICAL=0x01orso:Viewplaincopytoclipboardprint?01.LinearLayout.setOrientation(Integer.MAX_VALUE);02.LinearLayout.setOrientation(Integer.MIN_VALUE);03.LinearLayout.setOrientation(2012);LinearLayout.setOr
5、ientation(Integer.MAX_VALUE);LinearLayout.setOrientation(Integer.MIN_VALUE);LinearLayout.setOrientation(2012);sincetheparametersetOrientationreceivesisaninteger,youcanpassanylegitimateinteger-atleastthereisnoproblemincompiling.Itwillonlyatruntimemaycauseproblems,butasyouknow,develo
6、personlypayattentiontotheprocedurecanbecompiledsuccessfully,asfortheoperation,itistheuser'sconcern,becausedevelopersdonotnecessarilyusetheirdevelopedprogram.Inadditiontothisexample,thisAPIcanbeseeneverywhereinAndroid'sAPI,suchassettingViewvisibility,settingWifistatus,andsoon.Theyal
7、ldefineintegers,andthenuseintegersasarguments,andhopedeveloperscanpasstheconstantsdefinedbytheintegersetasparameters.Butasyouknow,noteveryonebehavessowell.Ifeveryoneobeystherules,theworldisreallyharmonious,andtheeggisfaraway.Becausedevelopersusuallyonlyfocusoncompiling,soifyoucanap
8、plythisruletocompile,theny