1. Enumeration class overview
The enum keyword, used to define an enumeration class, that is the same as the Class,interface keyword. An enumeration class is a special class.
But the enumeration class is not a normal class after all, it differs from the ordinary class in the following:
The ① enumeration class cannot inherit the class, but it can inherit the interface, and the enumeration class defined with the enum inherits the Java.lang.Enum class by default.
② using an enum to define a non-abstract enum class by default uses the final adornment, so the enumeration class cannot derive subclasses. If an abstract enumeration cannot be decorated with the "abstract" modifier, the compiler has its own idea.
The construction method of the ③ enumeration class can only be modified with private access, if the access modifier is omitted or the private modifier is default.
All enumerated instances of the ④ enumeration class must be placed at the top (top row) of the enumeration class, or the enumeration class cannot produce an instance. These enumeration instances automatically add the public static final modifier.
The enumeration instances in the ⑤ enumeration class are separated by commas (,), and when there is only an enumeration instance in the enumeration class, the last enumeration instance can be followed without a semicolon (;), otherwise a semicolon (;) must be added).
2. Use of enumeration classes2.1 Enumeration Classes can be used in a switch statement
Enum Color {red,green,blue}class Test {color color = color.red;public void change () {switch (color) {case RED:System.out. println ("I am Red"), Break;case GREEN:System.out.println ("I am Green"), Break;case BLUE:System.out.println ("I am Blue"); Default:System.out.println ("I don't know what color I Am");}}}
2.2 Enumeration Classes can add member variables and methods
Enum Color {red,green,blue;public int i;public void Play () {System.out.println ("I am Play Method");}}
2.3 Enumeration Classes can add construction methods
Enum Color {//is assigned by parentheses and must correspond to the owning construction method, otherwise the compilation error occurs. If you do not assign a value, you must have a parameterless construction method. Red ("Red"), Green ("green"), blue;private final String value; Color (String value) {This.value=value;} Color () {this.value= "color";} Public String GetValue () { return value; }}
2.4 Enumeration classes that implement abstract methods
Enum Color {RED {@Overridepublic string GetColor () {return "RED";}},green {@Overridepublic string GetColor () {return "Gree n ";}},blue {@Overridepublic String getColor () {return" BLUE ";}};/ /If an enumeration class has an abstract method enumeration instance must implement an abstract method. Public abstract String GetColor ();}
2.5 Enumeration classes that implement interfacesThere are two ways to implement the methods in an interface
① methods in implementing an interface in an enumeration class
Interface ienum{void play ();} Enum Color implements Ienum{red,green,blue; @Overridepublic void Play () {}}
② methods in implementing an interface in an enumeration instance
Interface ienum{void play ();} Enum Color implements ienum{red{public void Play () {}},green{public void play () {}},blue{public void Play () {}};}
2.5 Organizing enumeration classes using interfaces
Interface Icolor { enum Color implements Icolor{red,green,blue;}}
2.6 Traversal of enumerations
Class Test {public static void main (string[] args) { //Iterate through enumeration System.out.println ("traverse the value in the Color enumeration class"); Note: The enumeration class inherits Java.lang.Enum, so the following functions can be implemented for (Color color:Color.values ()) { System.out.println (color.value); } //Gets the number of instances of the enumeration System.out.println ("The enumeration instance in the Color enumeration class is" +color.values (). length+ "); Gets the index position of the enumeration, starting from 0 System.out.println (Color.RED.ordinal ());//0 System.out.println (Color.GREEN.ordinal ());//1 System.out.println (Color.BLUE.ordinal ());//2}} enum Color {red ("red"), Green ("green"), Blue ("blue"); Public final String value; Color (String value) {This.value=value;}}
3. Nature of the enumerationwe find enumerations and classes very much alike, and a little bit different. So how do we implement enumerations with classes?
Class color{public static final color red =new color ("RED"), public static final color green =new color ("GREEN"), public Static final color blue =new color ("BLUE"); Public final String value; Private Color (String value) {This.value=value;}}
This class is equivalent to the following enumeration class.
Enum Color {red ("red"), Green ("green"), Blue ("Blue");p ublic final String value; Color (String value) {This.value=value;}}
Java Enumeration Classes