After JDK 1.5, enum types can be defined with the help of enum keyword. The syntax structure is as follows:
[public] enum enumeration name [implements interface list]{ Enumerating object 1 [, enumerating object 2] [etc ]; [member variable 1;] [member variable 2;] [...] [(static or non static) code block] [construction method 1] [construction method 2] [...] [common method 1] [common method 2] [...] [abstract method 1] [abstract method 2] [...] }
We can see that enumeration objects are placed in the first line of valid code. Here, other code blocks or methods or properties are not allowed to be placed in front of enumeration objects. At the same time, there are several forms to define enumeration
public enum Color { RED, GREEN, BLUE;// Define three enumeration objects }
public enum Color { RED(1, "Red"), GREEN(2, "green"), BLUE(3, "blue"); //Property is a private property to ensure data security private int value; private String label; //Construction method is private type, public will make an error private Color(int value, String label) { this.value = value; this.label = label; } //Modifying and getting enumerations through public get and set methods public int getValue() { return value; } public String getLabel() { return label; } }
enum Color { RED { public String getLabel() { return "Red"; } }, GREEN { public String getLabel() { return "Green color"; } }; //Abstract methods can be defined directly in enumeration. At this time, each enumeration object needs to implement the abstract method public abstract String getLabel(); } public class Test { public static void main(String[] args) { //for each strengthen cycle for (Color color : Color.values()) { String label = color.getLabel(); System.out.println(label); } } }
For specific explanation, see the description above the code. At the same time, there are several ways to get enumeration
package com.test.csdn; enum Color { GREEN,BLUE,YELLOW; } public class Test { public static void main(String[] args) { //Get single directly // Color color = Color.BLUE; // System.out.println(color); //Get multiple enumeration objects Color []colors = Color.values(); for (Color color : colors) { System.out.println(color); } } }
The application of switch in enumeration
package com.test.csdn; enum Color { GREEN,BLUE,YELLOW; public static void print(Color color) { switch(color) { case GREEN: System.out.println("green"); break; case BLUE: System.out.println("blue"); break; case YELLOW: System.out.println("yellow"); break; } } } public class Test { public static void main(String[] args) { for(Color color : Color.values()) { Color.print(color); } } }
About enum keyword and enum class: enum class is an abstract class, which is the parent class of enumeration type in Java language, that is, enum type defined by enum keyword is equivalent to defining a subclass inheriting java.lang.Enum abstract class.
package com.test.csdn; enum Color { GREEN,BLUE,YELLOW; } public class Test { public static void main(String[] args) { for (Color color : Color.values()) { int ordinal = color.ordinal(); String name = color.name(); System.out.println(ordinal + " -> " + name); } } }
There are only so many things I know for the time being. I will add more later