Enum的通用写法


一. Enum的通用写法

  • 最近项目中Enum类的使用较多,借此机会将Enum类的常用方法和编程写法总结如下。
public enum TypeEnum {
    /**
     * 类型枚举实例
     */
    TYLE_1("1","类型1"),
    TYPE_2("2","类型2"),
    TYPE_3("3","类型3");

    TypeEnum(String code , String value){
        this.code = code ;
        this.value = value ;
    }

    private String code;
    private String value;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    /**
     * 通过code获取value
     * @param code
     * @return
     */
    public static String getValueByCode(String code){
        if(code == null){
            return "";
        }

        for(TypeEnum typeEnum:TypeEnum.values()){
            if(typeEnum.getCode().equals(code)){
                return typeEnum.getValue();
            }
        }
        return "";
    }

    /**
     * 通过code获取实例
     * @param code
     * @return
     */
    public static TypeEnum getTypeEnumBytypeId(String code){
        for(TypeEnum topType:values()){
            if(topType.getCode().equals(code)){
                return topType;
            }
        }
        return null;
    }
}

二. Enum在switch中的用法

  1. 特别注意:switch的case语句只能写枚举类定义的变量名,不能加类名。
  2. 错误范例
public static void testEnum(TypeEnum typeEnum){
   switch (typeEnum){
        case TypeEnum.TYLE_1:
            System.out.println("type1");
            break;
        case TypeEnum.TYPE_2:
            System.out.println("type2");
            break;
        case TypeEnum.TYPE_3:
            System.out.println("type3");
            break;
        default:
            System.out.println("其他");
            break;
    }
}
  • 上述代码编译会报错:an enum switch case label must be the unqualified name of an enumeration constant。其意思是枚举的switch...case标签必须为枚举常量的非限定名称。
  1. 正确范例
public class EnumTest {
    public static void main(String[] args) {
        String type = "1";
        TypeEnum typeEnum = TypeEnum.getTypeEnumBytypeId(type);
        if(typeEnum!=null){
            testEnum(typeEnum);
        }
    }

    public static void testEnum(TypeEnum typeEnum){
        switch (typeEnum){
            case TYLE_1:
                System.out.println("type1");
                break;
            case TYPE_2:
                System.out.println("type2");
                break;
            case TYPE_3:
                System.out.println("type3");
                break;
            default:
                System.out.println("其他");
                break;
        }
    }
}
  1. Tips:switch括号中的参数不能为null。如果swicth(null)会报java.lang.NullPointerException异常。

参考资料

如何在switch中使用enum

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值