public static void main(String[] args) {
// 根据类型获取描述
String desc = CarTypeEnum.getValue(1);
System.out.println("根据类型获取描述:" + desc);//宝马
// 根据描述获取类型
int type = CarTypeEnum.getType("奥迪");
System.out.println("根据描述获取类型:" + type);//3
}
/**
* @Classname CarTypeEnum
* @Description 汽车类型枚举
* @Author lw
* @Date 2020-01-08 11:32
*/
public enum CarTypeEnum {
CAR_TYPE_BMW(1, "宝马"),
CAR_TYPE_BC(2, "奔驰"),
CAR_TYPE_AUDI(3, "奥迪");
private int type;
private String desc;
private CarTypeEnum(int type, String desc) {
this.type = type;
this.desc = desc;
}
public static String getValue(int type) {
CarTypeEnum[] carTypeEnums = values();
for (CarTypeEnum carTypeEnum : carTypeEnums) {
if (carTypeEnum.type()==type) {
return carTypeEnum.desc();
}
}
return null;
}
public static int getType(String desc) {
CarTypeEnum[] carTypeEnums = values();
for (CarTypeEnum carTypeEnum : carTypeEnums) {
if (carTypeEnum.desc().equals(desc)) {
return carTypeEnum.type();
}
}
return Integer.parseInt(null);
}
private int type() {
return this.type;
}
private String desc() {
return this.desc;
}
}
java枚举
最新推荐文章于 2023-02-17 20:52:14 发布