Public classTestenum {/*the most common enumeration*/ Public enumcolorselect {red, green, yellow, blue; } /*enumerations can also add methods and properties like normal classes, and you can add static and non-static properties or methods to it, all as you do in a generic class.*/ Public enumSeason {//enum list must be written first, or compile errorwinter, Spring, summer, fall; Private Final StaticString location = "Phoenix"; Public StaticSeason getbest () {if(Location.equals ("Phoenix")) returnWinter; Else returnSummer; } } /*You can also have a construction method*/ Public enumTemp {/*The value is assigned by parentheses, and must have a parameter constructor and a property with the method, otherwise the compilation error * Assignment must be assigned or not assigned value, not part of the assignment is not assigned a value * if not assigned to the constructor, the assignment compiler also error*/Absolutezero (-459), freezing (+), boiling (212), Paperburns (451); Private Final intvalue; Public intGetValue () {returnvalue; } //constructors can only be private by default, ensuring that constructors are only used internallyTemp (intvalue) { This. Value =value; } } Public Static voidMain (string[] args) {/** An enumeration type is a type that defines a variable to restrict the assignment of a variable by "enumeration name. Value" To get the value in the relevant enumeration*/ColorSelect m=Colorselect.blue; Switch(m) {/*Note: Weightlifting wrote ToString (), saying that the value of the enumeration variable is not prefixed with * so it is blue instead of Colorselect.blue*/ 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; } System.out.println ("Traversing values in ColorSelect"); /*get an array of enumeration values by values ()*/ for(ColorSelect c:colorselect.values ()) {System.out.println (c); } System.out.println (The values in the enumeration ColorSelect are: "+colorselect.values (). length+" a "); /*ordinal () returns the index position of the enumeration value in the enumeration, starting at 0*/System.out.println (ColorSelect.red.ordinal ());//0System.out.println (ColorSelect.green.ordinal ());//1System.out.println (ColorSelect.yellow.ordinal ());//2System.out.println (ColorSelect.blue.ordinal ());//3 /*enumeration implements the Java.lang.Comparable interface by default*/System.out.println (ColorSelect.red.compareTo (Colorselect.green)); System.out.println (Season.getbest ()); for(Temp t:temp.values ()) {/*get the value of the associated enumeration by GetValue ()*/System.out.println (t+ "value is" +T.getvalue ()); } }}
enum Enum in Java