JAVA 列舉型別的替代
阿新 • • 發佈:2019-01-04
java 的enum型必須要 TeamTypeEnum.OFFICIAL 這樣子用才行,之前沒用過列舉,不會實現我想要的功能,就自己寫個湊合著用了。。。
偽列舉類:
/** * 型別, 1:民間 FOLK,2:校園 SCHOOL,3:企業 ENTERPRISE,4:機構 ORGANIZATION,5:官方 OFFICIAL * */ public class TeamTypeEnum { public static Integer getByKey(String key) throws IllegalTeamTypeException { int result = 0; switch (key) { case "FOLK": result = 1; break; case "SCHOOL": result = 2; break; case "ENTERPRISE": result = 3; break; case "ORGANIZATION": result = 4; break; case "OFFICIAL": result = 5; break; default: result = 0; break; } if(result == 0){ //throw new IllegalTeamTypeException(key); } return result; } }
使用:
int type = TeamTypeEnum.getByKey("OFFICIAL");//得到5