Java enumeration knowledge summary

basic concept:

Has not been a particularly clear enumeration of knowledge and application scenarios, specially take a moment to summarize the knowledge points. There are imperfect, welcome that;

JDK1.5 enum is a new feature. It is a special data type, it is possible to define a set of predefined constants as a variable. One of the variable must equal the value of its predefined. JDK1.6 also supported in the switch enumerated logical judgment.

Enumeration is defined by the enum keyword. The default is java.lang.Enum (Enum class belongs to the abstract) subclass. And each value is mapped to an enumerated type Enum constructor abstract class. Source as follows:

public abstract class Enum<E extends Enum<E>>
        implements Comparable<E>, Serializable {
    //Enum.java:117-120行
    protected Enum(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }
}

1, a simple version of the code is as follows :

/**
* TestEnum.java
* TestEnum 声明枚举
* RED,GREEN,BLUE 预定义的常量
**/
public enum TestEnum {
    RED,GREEN,BLUE;
}
//TestMain.java
class TestMain {
    public static void main(String[] args){
        for(TestEnum test : TestEnum.values()){
            System.out.println(test);
        }  
    }
}
//执行结果
RED
GREEN
BLUD

In fact, the above code execution three times Enum Constructor abstract class. Debugging can be interrupted at point

//类似执行了3次
/**
*第一个参数为值的名称
*第二个参数为值的序号
**/
new Enum<TestEnum>("RED",0);
new Enum<TestEnum>("GREEN",1);
new Enum<TestEnum>("BLUE",2);

2, the above code I do not know RED, GREEN, BLUE Gansha. Enumeration can achieve similar key: val form the structure of it? Possible, Java allows us to add any of the methods enumerated classes, the mechanism through which we can add so-called describes. code show as below:

/**
* TestEnum.java
* TestEnum 声明枚举
* RED,GREEN,BLUE 预定义的常量
**/
public enum TestEnum {
    RED("red","红色"),GREEN("green","绿色"),BLUE("blue","蓝色");
    private String code;
    private String desc;
    
    TestEnum(String code,String desc){
        this.code=code;
        this.desc=desc;
    }
    public String getCode(){
        return code;
    }
    public void setCode(String code){
        this.code=code;
    }
    
    public String getDesc(){
        return desc;
    }
    public void setDesc(String desc){
        this.desc=desc;
    }
    public static HashMap toMap(){ //枚举转成Map(前端的JSON)
        HashMap map=new HashMap();
        for(TestEnum test:TestEnum.values()){
            map.put(test.getCode(),test.getDesc());
        }
        return map;
    }
}
//TestMain.java
class TestMain {
    public static void main(String[] args){
        for(TestEnum test : TestEnum.values()){
            System.out.println(test.getDesc());
        }  
    }
}
//执行结果
红色
绿色
蓝色

The above manner can be used to make an HTTP request response returned JSON data format;

3, with the abstract class enumeration method; for example: different according to different class sizes, different numbers assigned teacher. A, B, C of these three classes. A class students to reach 30, the teacher assigned 2; 40 reaches students in the class B, the allocation of three teacher; 50 C Students who assigned 4 teacher; code to achieve the following:

//枚举文件TeacherNumEnum.java
public enum TeacherNumEnum{
    A("30人") {
      public int distribute(){
        return 2;
      }
    };
    B("40人") {
      public int distribute(){
        return 3;
      }
        
    };
    C("50人") {
      public int distribute(){
        return 4;
      }
    };
    private String desc; //人数描述
    TaskTypeEnum(String desc){
        this.desc=desc;
    }
    public String getDesc(){
        return desc;
    }
    public abstract int distribute();
}
//TestMain.java
class TestMain {
    public static void main(String[] args){
        for(TeacherNumEnum task : TeacherNumEnum.values()){
            System.out.println(task.getDesc()+";分配老师人数:"+task.distribute());
        }
    }
}
//执行结果
30;分配老师人数:2
40;分配老师人数:3
50;分配老师人数:4

4, there is no enumeration class inheritance, but can implement interfaces. Use extends realize there will be an error prompted the idea editor. Enumeration class implements the interface, is a constraint on the enumeration class. The code can itself;

Enum enumeration class Methods (TeacherNumEnum for example):

In this class the method Enum is divided into class and enumeration value method

//Enum类方法总结:
TeacherNumEnum.values()//Enum[]://返回Enum数组,所以for可以循环
TeacherNumEnum.valueof("枚举值"); //Enum:返回相应的枚举值
//Enum类值方法总结:
for(TeacherNumEnum task : TeacherNumEnum.values()){
    task.ordinal(); //int:返回枚举值相应的序号
    task.name(); //String:返回枚举值名称
    task.toString(); //String:返回枚举值名称字符串
    task.compareTo(E o); //int:返回此枚举与指定对象的顺序
    task.equals(E o); //boolean:返回此枚举与指定对象是否匹配
}

Guess you like

Origin blog.csdn.net/u012475786/article/details/90269036