java?е?????????÷?
???????????? ???????[ 2017/3/27 11:05:13 ] ??????????????????? Java
?????÷????????
??????JDK1.5??????????峣???????public static fianl....?????????????????????????????????鵽?????????????????????????????????????
????Java????
????publicenumColor{ RED??GREEN??BLANK??YELLOW }
?????÷?????switch
????JDK1.6????switch???????int??char??enum????????????????????????????????
????Java????
????enumSignal{ GREEN??YELLOW??RED } publicclassTrafficLight{ Signalcolor=Signal.RED; publicvoidchange(){ switch(color){ caseRED: color=Signal.GREEN; break; caseYELLOW: color=Signal.RED; break; caseGREEN: color=Signal.YELLOW; break; } } }
?????÷????????????????·???
???????????????????????????????????enum??????е????????????????Java???????????enum?????
????Java????
????publicenumColor{ RED("???"??1)??GREEN("???"??2)??BLANK("???"??3)??YELLO("???"??4); //??????? privateStringname; privateintindex; //?????? privateColor(Stringname??intindex){ this.name=name; this.index=index; } //??????? publicstaticStringgetName(intindex){ for(Colorc:Color.values()){ if(c.getIndex()==index){ returnc.name; } } returnnull; } //getset???? publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetIndex(){ returnindex; } publicvoidsetIndex(intindex){ this.index=index; } }
?????÷???????????????
??????????????toString()??????????????
????Java????
????publicenumColor{ RED("???"??1)??GREEN("???"??2)??BLANK("???"??3)??YELLO("???"??4); //??????? privateStringname; privateintindex; //?????? privateColor(Stringname??intindex){ this.name=name; this.index=index; } //??????? @Override publicStringtoString(){ returnthis.index+"_"+this.name; } }
?????÷??壺??????
???????е??????????java.lang.Enum??????Java???????У???????????????????????
????Java????
????publicinterfaceBehaviour{ voidprint(); StringgetInfo(); } publicenumColorimplementsBehaviour{ RED("???"??1)??GREEN("???"??2)??BLANK("???"??3)??YELLO("???"??4); //??????? privateStringname; privateintindex; //?????? privateColor(Stringname??intindex){ this.name=name; this.index=index; } //?????? @Override publicStringgetInfo(){ returnthis.name; } //?????? @Override publicvoidprint(){ System.out.println(this.index+":"+this.name); } }
?????÷???????y????????
????Java????
????publicinterfaceFood{ enumCoffeeimplementsFood{ BLACK_COFFEE??DECAF_COFFEE??LATTE??CAPPUCCINO } enumDessertimplementsFood{ FRUIT??CAKE??GELATO } }
?????÷??????????????????
????java.util.EnumSet??java.util.EnumMap?????????????EnumSet????????е??????????EnumMap?е? key??enum???????value??????????????????????????????????ò????????????????ο?JDK?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11