Application of JAVA enumeration type

  JDK1.5 introduced a new type - enumeration. Although it is a "small" function in Java, it is very helpful for development.

Related usage of enumeration

  Usage one:

   Before JDK1.5, we defined constants as: public static fianl…. . Well now, with the enumeration, you can put

  Related constants are grouped into an enumeration type, and enumerations provide more methods than constants.

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

  Usage two:

    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 three:

    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 an 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;  
     // Constructor   
    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;  
    }  
}  

More about ENUM usage reference:

  http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/

  https://blog.csdn.net/qq_27093465/article/details/52180865

Recommendations to follow when using ENUM

1. The enumeration class name is recommended to be suffixed with ENUM. The enumeration member name needs to be all uppercase, and the words are separated by underscores.
  Note: Enumeration is actually a special constant class, and the constructor is forced to be private by default.
  For example:
    Enumeration name: DealStatusEnum
    Member name: SUCCESS/UNKOWN_REASON
2. All enumeration type fields must have comments to explain the purpose of each data item
3. For value comparison between enumeration type objects, you can use ==, Directly compare the values ​​for equality, not necessarily using the equals method

4. The constructor of an enumeration class can only be private.

 

Guess you like

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