【MapSheep】
[好记性不如烂笔头]
posts - 228,comments - 15,views - 17万

枚举Switch

  1. 枚举类
package Test;
/**
* @program saas-rd-data-proxy
* @author: LouisVan
* @create: 2021/04/20 14:51
* @date 2021-04-20 14:57
* @author LouisVan
*/
public enum InfoNoticeType {
ITEM_BASE("itemBase", "商品主档"),
STOCK("stock", "库存"),
ITEM("item", "门店商品"),
PRICE("price", "价格"),
CATEGORY("category", "分类"),
STORE_CATEGORY("store_category", "门店个性分类"),
SHOP("shop", "店铺"),
;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
/**
* type 类型
*/
private String type;
/**
* desc 描述
*/
private String desc;
InfoNoticeType(String type, String desc) {
this.type = type;
this.desc = desc;
}
public static InfoNoticeType getInfoNoticeType(String type){
for(InfoNoticeType infoNoticeType: InfoNoticeType.values()){
if(infoNoticeType.getType().equals(type)){
return infoNoticeType;
}
}
return null;
}
}

  1. 测试类
@Test
public void option() {
String shopInfoNoticeReq = "{\n" +
" \"v\": \"v3\",\n" +
" \"format\": \"json\",\n" +
" \"sign\": \"e70a8e2a359544afa942e6918b6bdle6\",\n" +
" \"idList\": [\n" +
" {\n" +
" \"ids\": \"6914973606753|1108375\"\n" +
" }\n" +
" ],\n" +
" \"type\": \"price\",\n" +
" \"timestamp\": 1616574994683\n" +
"}";
ShopInfoNoticeReq shopInfoNotice = JSON.parseObject(shopInfoNoticeReq, new TypeReference<ShopInfoNoticeReq>() {
});
switch (InfoNoticeType.getInfoNoticeType(shopInfoNotice.getType())) {
case STOCK:
break;
case ITEM_BASE:
break;
case ITEM:
break;
case PRICE:
System.out.println("PRICE:" + InfoNoticeType.PRICE + "PRICE.getType():" +
InfoNoticeType.PRICE.getType() + "PRICE.getDesc()" + InfoNoticeType.PRICE.getDesc());
break;
case CATEGORY:
break;
case STORE_CATEGORY:
break;
case SHOP:
break;
default:
break;
}
}
// 输出: PRICE:PRICE PRICE.getType():price PRICE.getDesc():价格
posted on   (Play)  阅读(203)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~

点击右上角即可分享
微信分享提示