usage One: Constants Public enumMycolor{red,black,blue} Public enumColor {RED, GREEN, BLANK, YELLOW} enum is a keyword for an enumeration class, using a method similar to a static constant: MyColor m=Mycolor.black; use two:SwitchenumSignal {GREEN, YELLOW, RED} Public classtrafficlight {Signal color=signal.red; Public voidChange () {Switch(color) { CaseRed:color=Signal.green; Break; CaseYellow:color=signal.red; Break; CaseGreen:color=Signal.yellow; Break; } }} Public enumMycolor{red,black,blue} Public Static voidMain (string[] args) {MyColor m=Mycolor.black; Switch(m) { CaseRed:System.out.println ("MyColor is Red"); Break; CaseBlack:System.out.println ("MyColor is Black"); Break; default: System.out.println ("MyColor is Blue"); Break; } System.out.println (M); NOTE: The switch statement supports Byte, Short,int,Charthe enumeration type. Usage Three: To add a new method custom method to the enumeration, you must add a semicolon at the end of the enum instance sequence. and Java requirements must first be definedenuminstance. Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name;Private intindex;//Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index;} //Common Methods Public StaticString GetName (intindex) { for(Color c:color.values ()) {if(C.getindex () = =index) { returnC.name; } } return NULL; } //Get Set Method PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;} Public intGetIndex () {returnindex;} Public voidSetindex (intindex) { This. index =index;}} Usage Four: Overrides the enumeration method below gives an example of the ToString () method overlay. Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name;Private intindex;//Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index;} //override Method@Override PublicString toString () {return This. index+ "-" + This. Name;}} Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name; Private intindex; //Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index; } //override Method@Override PublicString toString () {return This. index+ "_" + This. Name; }} Use five: Implement an interface all enumerations inherit from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes. Public InterfaceBehaviour {voidprint (); String getInfo ();} Public enumColorImplementsbehaviour{RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name;Private intindex;//Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index;}//interface Method@Override PublicString GetInfo () {return This. Name; //interface Method@Override Public voidprint () {System.out.println ( This. index+ ":" + This. Name); }} Public InterfaceBehaviour {voidprint (); String getInfo ();} Public enumColorImplementsbehaviour{RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name; Private intindex; //Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index; }//interface Method@Override PublicString GetInfo () {return This. Name; } //interface Method@Override Public voidprint () {System.out.println ( This. index+ ":" + This. Name); }} Usage VI: Organizing enumerations using interfaces Public InterfaceFood {enumCoffeeImplementsfood{Black_coffee,decaf_coffee,latte,cappuccino}enumDessertImplementsfood{FRUIT, CAKE, GELATO}} Public InterfaceFood {enumCoffeeImplementsfood{Black_coffee,decaf_coffee,latte,cappuccino}enumDessertImplementsfood{FRUIT, CAKE, GELATO}} uses seven: The use of the enumeration collection, Java.util.EnumSet and Java.util.EnumMap, is a collection of two enumerations. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. Refer to the JDK documentation for the use of this two collection.
Common uses of java-enumerations