之前常規(guī)寫法:
@Getter
public enum InventoryTypeEnum {
All(0,"全部"),
ADD(1,"加料"),
BLOW(2,"放料");
private Integer id;
private String name;
InventoryTypeEnum(Integer id, String name) {
this.id = id;
this.name = name;
}
}
代碼會(huì)報(bào)錯(cuò):
此時(shí)在枚舉中添加方法:
public static InventoryTypeEnum toType(int id) {
return Stream.of(InventoryTypeEnum.values())
.filter(p -> p.id == id)
.findAny()
.orElse(null);
}
代碼中改為:
idea自編譯不報(bào)錯(cuò), 經(jīng)測(cè)試也可以用
枚舉完整代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-857904.html
@Getter
public enum InventoryTypeEnum {
All(0,"全部"),
ADD(1,"加料"),
BLOW(2,"放料");
private Integer id;
private String name;
InventoryTypeEnum(Integer id, String name) {
this.id = id;
this.name = name;
}
public static InventoryTypeEnum toType(int id) {
return Stream.of(InventoryTypeEnum.values())
.filter(p -> p.id == id)
.findAny()
.orElse(null);
}
}
參考:?「Java基礎(chǔ)入門」Java中switch怎么使用枚舉 - 掘金文章來源地址http://www.zghlxwxcb.cn/news/detail-857904.html
到了這里,關(guān)于java jdk8 switch case中無法使用枚舉問題解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!