Enumeration meaning, purpose, function, usage, action scenes

Example properties and methods declared in the enumeration given below:

package com.fhp.enumexample;
 
public enum TypeEnum {
    VIDEO(1), AUDIO(2), TEXT(3), IMAGE(4);
    
    int value;
    
    TypeEnum(int value) {
        this.value = value;
    }
    
    public int getValue() {
        return value;
    }
}

In this enumeration, int type values ​​for each field has a corresponding enumeration, and enumeration of different values ​​will have different values ​​int. At the same time, it is ordinary class, you can declare the constructor and a variety of methods. Such as:

TypeEnum type = TypeEnum.TEXT;//type的value属性值为3。
System.out.println(type.getValue());//屏幕输出3。

If you want to specify attributes for each enumeration value must be declared attribute parameter corresponding to a type of construction method (is not public) in the enumeration. Otherwise the compiler will give The constructor TypeEnum (int, String) the error is undefined. In this embodiment, the attribute is an int, a method should be configured so int. In addition, the enumeration can also specify multiple attributes, such as:

package com.fhp.enumexample;
 
public enum TypeEnum {
    VIDEO(1, "视频"), AUDIO(2, "音频"), TEXT(3, "文本"), IMAGE(4, "图像");
    
    int value;
    String name;
    
    TypeEnum(int value, String name) {
        this.value = value;
        this.name = name;
    }
    
    public int getValue() {
        return value;
    }
    
    public String getName() {
        return name;
    }
}

enum also built a number of methods, commonly used as follows:

int compareTo(E o) 
          比较此枚举与指定对象的顺序。


Class<E> getDeclaringClass() 
          返回与此枚举常量的枚举类型相对应的 Class 对象。


String name() 
          返回此枚举常量的名称,在其枚举声明中对其进行声明。


int ordinal() 
          返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。


String toString()
           返回枚举常量的名称,它包含在声明中。


static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 
          返回带指定名称的指定枚举类型的枚举常量。


static T[] values()

返回该枚举的所有值。

Now, it is assumed to implement a method of generating enumeration enumeration value, it can be done according to the integer value:

package com.fhp.enumexample;
 
public enum TypeEnum {
    VIDEO(1, "视频"), AUDIO(2, "音频"), TEXT(3, "文本"), IMAGE(4, "图像");
    
    int value;
    String name;
    
    TypeEnum(int value, String name) {
        this.value = value;
        this.name = name;
    }
    
    public int getValue() {
        return value;
    }
    
    public String getName() {
        return name;
    }
 
    public static TypeEnum getByValue(int value) {
        for(TypeEnum typeEnum : TypeEnum.values()) {
            if(typeEnum.value == value) {
                return typeEnum;
            }
        }
        throw new IllegalArgumentException("No element matches " + value);
    }
}

getByValue (int) is the integer value of the numerical method for the enumeration revolution. Calling values ​​() method to get all the enumerated values, then for each of the enumerated below and given integer value matches, if the matching is directly returned, if no matching value IllegalArgumentException exception is thrown, a parameter indicating illegal, both the role of validation.

To sum up, we can see that in the enumeration JDK5 newly introduced the perfect solution to the problem of discrete brought before represented by a constant, greatly enhance the readability, ease of use and maintainability of the program, and on this basis it has been extended so that it can, like classes to use, it is up to a new level for the Java representation of discrete quantity. Therefore, if the need to represent in Java, such as limited color, fashion, category, status, etc. The number, in the form of discrete, yet very clear expression of the amount, should try to abandon the practice of constant representation, and will enumerate as a primary choice.
----------------
Disclaimer: This article is CSDN blogger original article "Jicky-17", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https://blog.csdn.net/u014527058/article/details/52751488

Guess you like

Origin www.cnblogs.com/jsll/p/11613032.html