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();

猜你喜欢

转载自blog.csdn.net/cliper9768/article/details/85091951