JAVA 枚举类型的替代


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


你可能感兴趣的:(JAVA 枚举类型的替代)