方案一:
package com.service.enums; public enum ViabilitylevelEnum { IndependentType("ebd72234","独立","IL","a0489ec8","0"), LifeCareLevelOne("9989cd22","生活1级","AL1","37ed42fc","1"), LifeCareLevelTwo("1a989df8","生活2级","AL2","91f4894a","2"), LifeCareLevelThree("4ecfdbbf","生活3级","AL3","080a308a","3"), LifeCareLevelFour("f39d1d73","生活4级","AL4","81727a2f","4"), MentalRetardationOne("43756ba0","失智1级","MC1","37ed42fc","1"), MentalRetardationTwo("d43746b3","失智2级","MC2","91f4894a","2"), MentalRetardationThree("dbc6b74d","失智3级","MC3","080a308a","3"), MentalRetardationFour("996d0be8","失智4级","MC4","81727a2f","4"); private String code; private String name; private String no; private String serviceLevelCode; private String serviceLevelName; ViabilitylevelEnum(String code, String name,String no,String serviceLevelCode,String serviceLevelName) { this.code = code; this.name = name; this.no=no; this.serviceLevelCode=serviceLevelCode; this.serviceLevelName=serviceLevelName; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getServiceLevelCode() { return serviceLevelCode; } public void setServiceLevelCode(String serviceLevelCode) { this.serviceLevelCode = serviceLevelCode; } public String getServiceLevelName() { return serviceLevelName; } public void setServiceLevelName(String serviceLevelName) { this.serviceLevelName = serviceLevelName; } public static ViabilitylevelEnum getById(String name){ for(ViabilitylevelEnum type : values()){ if (type.getName().equals(name)) { //获取指定的枚举 return type; } } return null; }
public CodeNameDto getLevelByViabilityLevelName(String viabilityLevelName) { CodeNameDto cnd=new CodeNameDto(); switch (ViabilitylevelEnum.getById(viabilityLevelName)){ case IndependentType: cnd.setName(ViabilitylevelEnum.IndependentType.getName()); cnd.setCode(ViabilitylevelEnum.IndependentType.getCode()); break; case LifeCareLevelOne: System.out.println(""); break; case LifeCareLevelTwo: System.out.println(""); break; case LifeCareLevelThree: System.out.println(""); break; case LifeCareLevelFour: System.out.println(""); break; case MentalRetardationOne: System.out.println(""); break; case MentalRetardationTwo: System.out.println(""); break; case MentalRetardationThree: System.out.println(""); break; case MentalRetardationFour: System.out.println(""); break; default: break; } return cnd; }
方案二:
package Enum;
/**
* @Author:lin
* @Description:switch中使用枚举
* @Date:16:46 2018/4/20
* @Modified by:
*/
public enum OpCodeEnum {
ADD("加"),
SUB("减"),
MUL("乘"),
DIV("除"),
INC("自增"),
DEC("自减"),
LT("小于"),
GT("大于"),
EQ("等于"),
ISZERO("是否为0"),
NULL("null"),
NOT("不等于"),
RETURN("返回"),
MOD("取余"),
AND("并且"),
OR("或者");
private String msg;
OpCodeEnum(String msg) {
this.msg = msg;
}
/*
* 匹配操作码
* */
public static OpCodeEnum matchOpCode(String opCodeStr) {
for (OpCodeEnum opCode : OpCodeEnum.values()) {
if (opCode.name().equalsIgnoreCase(opCodeStr)) {
return opCode;
}
}
return OpCodeEnum.NULL;
}
public static void main(String[] args) {
//switch中使用枚举
String opStr = "add";
switch (OpCodeEnum.matchOpCode(opStr)) {
case ADD:
System.out.println("加法");
break;
case SUB:
System.out.println("减法");
break;
case MUL:
System.out.println("乘法");
break;
case DIV:
System.out.println("除法");
break;
}
}
}