枚举Switch

枚举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 = "{
" +
			"    "v": "v3",
" +
			"    "format": "json",
" +
			"    "sign": "e70a8e2a359544afa942e6918b6bdle6",
" +
			"    "idList": [
" +
			"        {
" +
			"            "ids": "6914973606753|1108375"
" +
			"        }
" +
			"    ],
" +
			"    "type": "price",
" +
			"    "timestamp": 1616574994683
" +
			"}";

	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():价格
原文地址:https://www.cnblogs.com/Twittery/p/14691013.html