Java enumeration of enum details seven common USES

  • 2020-05-17 05:34:46
  • OfStack

JDK 1.5 introduces a new type -- enumeration. While it's a "small" feature in Java, it makes my development a "big" convenience.

Usage 1: constants

Before JDK 1.5, we defined constants as: public static fianl... . Now, with enums, you can group related constants into one enum type, and enums provide more methods than constants.


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

Usage 2: switch

switch statements before JDK1.6 only support int,char,enum types. Enums 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 enumeration

If you are going to customize your own method, you must add a semicolon at the end of the enum instance sequence. Also, Java requires that an enum instance be defined first.


 public enum Color { 
   RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4); 
   //  Member variables  
   private String name; 
   private int index; 
   //  A constructor  
   private Color(String name, int index) { 
     thisname = name; 
     thisindex = index; 
   } 
   //  Common methods  
   public static String getName(int index) { 
     for (Color c : Colorvalues()) { 
       if (cgetIndex() == index) { 
         return cname; 
       } 
     } 
     return null; 
   } 
   // get set  methods  
   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: override the enumeration method

Here is an example of an toString() method override.


 public enum Color { 
   RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4); 
   //  Member variables  
   private String name; 
   private int index; 
   //  A constructor  
   private Color(String name, int index) { 
     thisname = name; 
     thisindex = index; 
   } 
   // Override method  
   @Override 
   public String toString() { 
     return thisindex+"_"+thisname; 
   } 
 } 

Usage 5: implement the interface

All enumerations inherit from the java.lang.Enum class. Since Java does not support multiple inheritance, enumeration objects can no longer inherit from 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 variables  
   private String name; 
   private int index; 
   //  A constructor  
   private Color(String name, int index) { 
     thisname = name; 
     thisindex = index; 
   } 
 // Interface methods  
   @Override 
   public String getInfo() { 
     return thisname; 
   } 
   // Interface methods  
   @Override 
   public void print() { 
     Systemoutprintln(thisindex+":"+thisname); 
   } 
 } 

Usage 6: use interfaces to organize enums


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 enumerated collections

java.util.EnumSet and java.util.EnumMap are two enumerated collections. EnumSet guarantees that elements in the collection are not repeated; key in EnumMap is of type enum, while value can be of any type. The use of these two collections is not described here, but can be referred to the JDK documentation.

For implementation details and principles of enumeration, please refer to:

Resources: the fourth edition of the ThinkingInJava / / www ofstack. com books / 75540. html


Related articles: