Java enum枚举配合switch使用
2023-03-07 09:44:12 时间
一看就懂,一写就忘
定义枚举
public enum TypeEnum {
//
type1(1, "水果"),
type2(2, "蔬菜"),
type3(3, "零食");;
private final Integer code;
private final String value;
TypeEnum(Integer code, String value) {
this.code = code;
this.value = value;
}
public Integer getCode() {
return code;
}
public String getValue() {
return value;
}
// 根据code返回枚举类型,主要在switch中使用
public static TypeEnum getByCode(Integer code) {
for (TypeEnum optionTypeEnum : values()) {
if (optionTypeEnum.getCode().equals(code)) {
return optionTypeEnum;
}
}
return null;
}
}
test
@Test
public void test(){
System.out.println(TypeEnum.getByCode(1).getValue());
System.out.println(TypeEnum.type1.getCode() + "_" + TypeEnum.type1.getValue());
System.out.println(TypeEnum.type2.getCode()+ "_" + TypeEnum.type2.getValue());
System.out.println(TypeEnum.type3.getCode()+ "_" + TypeEnum.type3.getValue());
switch (TypeEnum.getByCode(1)){
case type1:
System.out.println("吃水果");break;
case type2:
System.out.println("吃蔬菜"); break;
case type3:
System.out.println("吃零食");break;
}
}
结果
相关文章
- 做六西格玛,如何挑选咨询公司?
- 以 standalone 模式启动 Aapche Pulsar
- 博通610亿美元收购VMware交易遇阻,欧盟将启动反垄断调查
- 复旦大学研发出晶圆级异质CFET技术:相同工艺节点下,可实现器件集成密度翻倍
- 大变局下,中国大陆存储芯片产业如何走出至暗时刻
- Tower for Mac(强大的Git客户端)
- 中国信科“1.6Tb/s硅光互连芯片”成功入选十项重大科技进展
- “芯”款来袭,中移芯昇科技发布首款RISC-V内核NB-IoT和LTE Cat.1bis通信芯片
- 因产线升级及需求放缓,传特斯拉上海工厂将于12月底暂时停产!内部人士回应
- 小米13系列正式发布:徕卡三摄+骁龙8 Gen 2,3999元起售
- 台积电明年一季度7nm产能利用率或将跌破50%
- 传Twitter大砍80%服务器订单,英特尔很受伤
- 全球半导体风险投资额骤降46%!有创业公司破产,也人有逆势出击!
- 客户恢复拉货,被动元件需求回暖
- 中国已向世贸组织提起诉讼:要求美国停止扰乱芯片等高科技产品贸易
- SEMI报告:预计到2024年全球半导体行业新工厂投资将超过5000亿美元
- 台积电公布将模拟设计迁移到3nm的方法
- “万亿补贴”小作文再现,这是“出口转内销”?
- Prism 9 for Mac 9.5.1(医学绘图软件)
- RISC-V杀向服务器市场!5nm制程、最高192核,性能超AMD EPYC 7763!