Usage 1: Constant
Before JDK1.5, we defined constants: publicstaticfianl.... Now that with the enum, you can group the relevant constants into an enum type, and enums provide more methods than constants.
Java code
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.
Java code
enum Signal {GREEN, YELLOW, RED}public class TrafficLight {Signal color = Signal.RED;public void change() {switch (color) {case RED:color = Signal.GREEN;break;case YELLOW:color = Signal.RED;break;case GREEN:color = Signal.YELLOW;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.
Java code
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) {this.name = name;this.index = index;}// Normal method public static String getName(int index) {for (Color c : Color.values()) {if (c.getIndex() == index) {return c.name;}}return null;}// get set method public String getName() {return name;}public void setName(String name) {this.name = name;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}}Usage 4: Methods to overwrite enumeration
Here is an example of the toString() method override.
Java code
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) {this.name = name;this.index = index;}// Overwrite method @Overridepublic String toString() {return this.index+"_"+this.name;}}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.
Java code
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) {this.name = name;this.index = index;}// Interface method @Overridepublic String getInfo() {return this.name;}//Interface method @Overridepublic void print() {System.out.println(this.index+":"+this.name);}}Usage 6: Use interface to organize enumeration
Java code
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, please refer to the JDK documentation.
The above is the summary of seven common usages of Java enumerations (must read) brought to you by the editor. I hope you will support Wulin.com more~