JDK1.5 introduces a new type - enumeration. Although it is considered a "small" function in Java, it brings "big" convenience to my development.
Usage 1: Constant
Before JDK1.5, we defined constants: public static fianl...... . Now that with the enum, you can group the relevant constants into an enum type, and enums provide more methods than constants.
public enum Color { RED, GREEN, BLANK, YELLOW }Usage 2: switch
The switch statements before JDK1.6 only support int, char, and enum types. The use of enums can make our code more readable.
enum Signal { GREEN, YELLOW, RED } public class TrafficLight { Signal color = SignalRED; public void change() { switch (color) { case RED: color = SignalGREEN; break; case YELLOW: color = SignalRED; break; case GREEN: color = SignalYELLOW; break; } } }Usage 3: Add a new method to the enum
If you plan to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that the enum instance must be defined first.
public enum Color { RED("red", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4); // Member variable private String name; private int index; // Construct method private Color(String name, int index) { thisname = name; thisindex = index; } // Normal method public static String getName(int index) { for (Color c : Colorvalues()) { if (cgetIndex() == index) { return cname; } } return null; } // get set method public String getName() { return name; } public void setName(String name) { thisname = name; } public int getIndex() { return index; } public void setIndex(int index) { thisindex = index; } }Usage 4: Methods to overwrite enumeration
Here is an example of the toString() method override.
public enum Color { RED("red", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4); // Member variable private String name; private int index; // Construct method private Color(String name, int index) { thisname = name; thisindex = index; } // Overwrite method @Override public String toString() { return thisindex+"_"+thisname; } }Usage 5: Implement the interface
All enums are inherited from the java.lang.Enum class. Since Java does not support multiple inheritance, enum objects cannot inherit other classes.
public interface Behaviour { void print(); String getInfo(); } public enum Color implements Behaviour{ RED("red", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4); // Member variable private String name; private int index; // Construct method private Color(String name, int index) { thisname = name; thisindex = index; } // Interface method @Override public String getInfo() { return thisname; } //Interface method @Override public void print() { Systemoutprintln(thisindex+":"+thisname); } }Usage 6: Use interface to organize enumeration
public interface Food { enum Coffee implements Food{ BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO } enum Dessert implements Food{ FRUIT, CAKE, GELATO } }Usage 7: About the use of enumeration collections
java.util.EnumSet and java.util.EnumMap are two enum collections. EnumSet ensures that elements in the collection are not repeated; the key in EnumMap is of type enum, and value can be of any type. I will not elaborate on the use of these two sets here, you can refer to the JDK documentation.
For the implementation details and principles of enumeration, please refer to:
Reference: "ThinkingInJava" Fourth Edition //www.VeVB.COM/books/75540.html
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.