知识点:
1.枚举的优雅创建
2.values()遍历枚举中的常量值
3.valueof(String name) :根据名称获取枚举类中定义的常量值;要求字符串跟枚举的常量名必须一致;
下面是枚举创建:
package **;
import com.baomidou.mybatisplus.core.enums.IEnum;
import java.io.Serializable;
public enum ReturnTypeEnum implements IEnum {
SUCCESS("S","成功"),
ERROR("E","失败"),
WARNING("W","警告");
private String id;
private String desc;
ReturnTypeEnum(String id, String desc) {
this.id = id;
this.desc = desc;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public Serializable getValue() {
for (ReturnTypeEnum returnTypeEnum:values()){
if (returnTypeEnum.getId().equals(id)){
return returnTypeEnum;
}
}
return null;
}
}
public class EnumTest {
8 public static void main(String[] args){
9 /**
10 * 测试枚举的values()
11 *
12 */
13 String s = Color.getValue("01").getDesc();
14 System.out.println("获取的值为:"+ s);
15
16
17
18 /**
19 * 测试枚举的valueof,里面的值可以是自己定义的枚举常量的名称
20 * 其中valueOf方法会把一个String类型的名称转变成枚举项,也就是在枚举项中查找字面值和该参数相等的枚举项。
21 */
22
23 ReturnTypeEnum returnTypeEnum =ReturnTypeEnum.valueOf("SUCCESS");
24 System.out.println(returnTypeEnum.getDesc());
25
26 /**
27 * 测试枚举的toString()方法
28 */
29
30 ReturnTypeEnum returnTypeEnum2 = returnTypeEnum.getValue("01") ;
31 System.out.println("获取的值为:"+ returnTypeEnum2.toString());
32
33 }
标签:java,定义,public,String,枚举,ReturnTypeEnum,id,desc
From: https://www.cnblogs.com/leleChang/p/18317703