Enum枚举通过code获取到message三中方法

在开发中遇到Enum的code获取message的问题,于是各种百度,找一个3中方法
第一种最low的方法,不推荐使用

@Getter
public enum PaymentTypeEnum {
    PAY_ONLINE(1, "在线支付");
    private Integer code;
    private String message;
 
    PaymentTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
 
    public static PaymentTypeEnum getPaymentType(int value) {
        for (PaymentTypeEnum paymentTypeEnum : PaymentTypeEnum.values()) {
            if (value == paymentTypeEnum.getCode()) {
                return paymentTypeEnum;
            }
        }
        return null;
    }
}

在枚举的类中添加一个getPaymentType的方法,这里最不推荐
第二中方法,网上找类的方法通用性好

@Getter
public enum PaymentTypeEnum {
    PAY_ONLINE(1, "在线支付");
    private Integer code;
    private String message;
 
    PaymentTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

主要代码

public static <T extends Enum<T>> T getByIntegerTypeCode(Class<T> clazz,String getTypeCodeMethodName, Integer typeCode){
        T result = null;
        try{
            //Enum接口中没有values()方法,我们仍然可以通过Class对象取得所有的enum实例
            T[] arr = clazz.getEnumConstants();
            //获取定义的方法
            Method targetMethod = clazz.getDeclaredMethod(getTypeCodeMethodName);
            Integer typeCodeVal = null;
            //遍历枚举定义
            for(T entity:arr){
                //获取传过来方法的
                typeCodeVal = Integer.valueOf(targetMethod.invoke(entity).toString());
                //执行的方法的值等于参数传过来要判断的值
                if(typeCodeVal.equals(typeCode)){
                    //返回这个枚举
                    result = entity;
                    break;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return result;
    }

使用

PaymentTypeEnum paymentTypeEnum=getByIntegerTypeCode(PaymentTypeEnum.class,"getCode",1);
String message=paymentTypeEnum.getMessage();

类的完整代码,百度盘链接:https://pan.baidu.com/s/1kUOBYpp 密码:2k4l
第三中方法,也是通过反射到枚举对象,这个跟上面的类似,只适用单个项目,视频上看到的

定义一个CodeEnum接口,所有要反射得到message都要implements 实现这个接口

public interface CodeEnum {
    Integer getCode();
}

我的枚举修改成

@Getter
public enum PaymentTypeEnum implements CodeEnum{
    PAY_ONLINE(1, "在线支付");
    private Integer code;
    private String message;
 
    PaymentTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

添加实现一个CodeEnum的接口

Enum工具类

public class EnumUtil {
    //返回的对象实现CodeEnum接口    
    public static <T extends CodeEnum> T getByCode(Class<T> enumClass, Integer code) {
        for (T each : enumClass.getEnumConstants()) {
            if(each.getCode()==code){
                return each;
            }
        }
        return null;
    }
}

使用方法

PaymentTypeEnum paymentTypeEnum= EnumUtil.getByCode(PaymentTypeEnum.class,1);
String message=paymentTypeEnum.getMessage();
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值