相关链接:https://blog.youkuaiyun.com/weixin_30760895/article/details/95742267
话不多说,先上代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StoreStock implements Serializable {
/**
* 门店id
*/
private Long storeId;
/**
* 指定范围
**/
private Integer applyRange;
/**
* 城市ID
**/
private Long cityId;
}
枚举类:
public enum ApplyRangeEnum {
//指定范围 1-全部城市;2-指定城市;3-指定门店
ALL_CITY(1,"全部城市"),
PART_CITY(2,"指定城市"),
PART_STORE(3,"指定门店");
private Integer code;
private String msg;
ApplyRangeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
public Boolean check(Integer code) {
return this.getCode().equals(code);
}
}
public static void main(String[] args) {
StoreStock storeStock=new StoreStock();
storeStock.setApplyRange(3);
//
System.out.println(ApplyRangeEnum.ALL_CITY.check(storeStock.getApplyRange())?Long.valueOf(-1L):storeStock.getCityId());
//空指针 自动拆箱 或报空指针
System.out.println(ApplyRangeEnum.ALL_CITY.check(storeStock.getApplyRange())?-1L:storeStock.getCityId());
}