Package com.ljq.test;/** * Enumeration Usage Detailed * * @author Jiqinlin **/ Public classTestenum {/** * Normal enumeration * * @author Jiqinlin **/ Public enumcolorenum {red, green, yellow, blue; } /** * Enumeration like a normal class can add properties and methods, you can add static and non-static properties or methods to it * * @author Jiqinlin **/ Public enumSeasonenum {//Note: The enumeration is written first, otherwise the compilation errorspring, summer, autumn, winter; PrivateFinalStaticString position ="Test"; Public Staticseasonenum Getseason () {if("Test". Equals (position))returnSpring; Else returnWinter; } } /** * Gender * * Implement enumerations with constructors * * @author Jiqinlin **/ Public enumgender{//assign a value through parentheses, and must have a parameter constructor and a property and method, or compile an error//assignments must either be assigned or not assigned, and cannot be assigned a part of the value, or the constructor cannot be written if no value is assigned, and the assignment compiles with errorsMans ("Mans"), Women ("Women"); Privatefinal String value; //constructors can only be private by default, ensuring that constructors are only used internallyGender (String value) { This. Value =value; } PublicString GetValue () {returnvalue; } } /** * Order Status * * Implement enumerations with abstract methods * * @author Jiqinlin **/ Public enumOrderstate {/** Canceled*/CANCEL { PublicString GetName () {return "has been canceled";}}, /** Pending Review*/waitconfirm { PublicString GetName () {return "Pending Review";}}, /** Waiting for payment*/waitpayment { PublicString GetName () {return "Waiting for payment";}}, /** Distribution is in stock*/admeasureproduct { PublicString GetName () {return "Distribution in progress";}}, /** Awaiting delivery*/Waitdeliver { PublicString GetName () {return "Awaiting shipment";}}, /** Shipped*/delivered { PublicString GetName () {return "already shipped";}}, /** Goods received*/RECEIVED { PublicString GetName () {return "Goods received";}}; Public AbstractString getName (); } Public Static voidMain (string[] args) {//An enumeration is a type that defines a variable to limit the assignment of a variable, and a value in an enumeration that is obtained through the enumeration name. Value when assigned.Colorenum Colorenum =Colorenum.blue; Switch(colorenum) { CaseRed:system. out. println ("color is red"); Break; CaseGreen:system. out. println ("color is green"); Break; CaseYellow:system. out. println ("Color is yellow"); Break; CaseBlue:system. out. println ("color is blue"); Break; } //Traversal EnumerationSystem. out. println ("iterating through the values in the Colorenum enumeration"); for(Colorenum color:ColorEnum.values ()) {System. out. println (color); } //gets the number of enumerationsSystem. out. println ("the values in the Colorenum enumeration are"+colorenum.values (). length+"a"); //gets the index position of the enumeration, starting from 0 by defaultSystem. out. println (ColorEnum.red.ordinal ());//0System. out. println (ColorEnum.green.ordinal ());//1System. out. println (ColorEnum.yellow.ordinal ());//2System. out. println (ColorEnum.blue.ordinal ());//3//enumeration implements the Java.lang.Comparable interface by defaultSystem. out. println (ColorEnum.red.compareTo (Colorenum.green));//-1//--------------------------System. out. println ("==========="); System.err.println ("season for"+Seasonenum.getseason ()); //--------------System. out. println ("==========="); for(Gender gender:Gender.values ()) {System. out. println (Gender.value); } //--------------System. out. println ("==========="); for(Orderstate order:OrderState.values ()) {System. out. println (Order.getname ()); } } }
usage Three: Add a new method to the enumeration if you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance be defined first. Java Code 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; }}
Java enumeration using detailed