switch中不能直接匹配枚举值,例如:
switch (order.getOrderType) {
case OrderEnum.ORDER_OF_GOODS.getValue :
}
解决方法为在枚举类中新增匹配枚举值方法,使用此方法:
switch (OrderEnum.of(order.getOrderType())) {
case ORDER_OF_GOODS:
}
package com.mtech.farmmall.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 订单相关枚举类
* @since 2022-12-5
*/
@Getter
@AllArgsConstructor
public enum OrderEnum {
/**
* 订单类型
*/
ORDER_OF_GOODS("商品订单","1"),
ORDER_PRE_SALE("预售订单","2"),
ORDER_RECIPE("食谱订单","3"),
;
final String name;
final String value;
/**
* 匹配枚举值
* @param value 值
* @return OrderEnum
*/
public static OrderEnum of(String value) {
if (value == null) {
return null;
}
OrderEnum[] values = OrderEnum.values();
for (OrderEnum a : values) {
if (a.value.equals(value)) {
return a;
}
}
return null;
}
}