Seven common uses of Java enumeration (enum)

Usage 1: Constants

Before JDK1.5, we defined constants: public static fianl…. . Well now, with enums, related constants can be grouped into an enum type, and enums provide more methods than constants.

public enum Color {  
 RED, GREEN, BLANK, YELLOW  
}


Usage 2: switch

The switch statement before JDK1.6 only supports int, char, and enum types. Using enumeration can make our code more readable.

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 an enumeration

If you plan to customize your own methods, 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("红色"1), GREEN("绿色"2), BLANK("白色"3), YELLO("黄色"4);  
   // 成员变量  
   private String name;  
   private int index;  
   // 构造方法  
   private Color(String name, int index{  
       this.name = name;  
       this.index = index;  
   }  
   // 普通方法  
   public static String getName(int index{  
       for (Color c : Color.values()) {  
           if (c.getIndex() == index) {  
               return c.name;  
           }  
       }  
       return null;  
   }  
   // get set 方法  
   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: Override the enumeration method

An example of toString() method override is given below.

public enum Color {  
   RED("红色"1), GREEN("绿色"2), BLANK("白色"3), YELLO("黄色"4);  
   // 成员变量  
   private String name;  
   private int index;  
   // 构造方法  
   private Color(String name, int index) {  
       this.name = name;  
       this.index = index;  
   }  
   //覆盖方法  
   @Override  
   public String toString() {  
       return this.index+"_"+this.name;  
   }  
}


Usage 5: Implementing an interface

All enums inherit from java.lang.Enumclasses. Since Java does not support multiple inheritance, enum objects can no longer inherit from other classes.

public interface Behaviour {  
   void print();  
   String getInfo();  
}  
public enum Color implements Behaviour{  
   RED("红色"1), GREEN("绿色"2), BLANK("白色"3), YELLO("黄色"4);  
   // 成员变量  
   private String name;  
   private int index;  
   // 构造方法  
   private Color(String name, int index) {  
       this.name = name;  
       this.index = index;  
   }  
   //接口方法  
   @Override  
   public String getInfo() {  
       return this.name;  
   }  
   //接口方法  
   @Override  
   public void print() {  
       System.out.println(this.index+":"+this.name);  
   }  
}


Usage 6: Use interfaces to organize enumerations


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 sets

java.util.EnumSetand java.util.EnumMapare two enumerated collections. EnumSet ensures that the elements in the collection are not repeated; the key in EnumMap is of the enum type, and the value can be of any type. The use of these two sets will not be repeated here, you can refer to the JDK documentation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641067&siteId=291194637