赞
踩
要点:
1、先通过枚举的getEnumByType方法获取具体的枚举
2、case选项设置为具体枚举
package com.lhh.myspringboot.constant; import lombok.Getter; import java.util.Arrays; @Getter public enum DiscountModeEnum { ONE_PRICE(1,"描述1"), DISCOUNT(2,"描述2"), REDUCE(3,"描述3"); private final Integer type; private final String desc; DiscountModeEnum(Integer type, String desc){ this.type = type; this.desc = desc; } public static DiscountModeEnum getEnumByType(Integer type){ if (type == null){ return null; } return Arrays.stream(DiscountModeEnum.values()) .filter(discountModeEnum -> discountModeEnum.type.equals(type)) .findFirst().orElse(null); } }
Integer type = 1;
// 1、先通过枚举的getEnumByType方法获取具体的枚举
DiscountModeEnum modeEnum = DiscountModeEnum.getEnumByType(type)
switch(modeEnum){
// 2、case选项设置为具体枚举
case ONE_PRICE: System.out.println("ONE_PRICE"); break;
case DISCOUNT: System.out.println("DISCOUNT"); break;
case REDUCE: System.out.println("REDUCE"); break;
default:System.out.println("DEFAULT");
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。