枚举的基本用法
时间:2020年10月22日09:57:40
- 老鸟建议
- 不要使用枚举的高级特性,如果当你都用高级特性的时候,那就不如把这个类定义成为一个普通类。不要那么麻烦了。
- 当你需要一组常量的时候,可以使用枚举类型。
- 枚举是用来方便的,不是用来搞特殊的。
- 什么时候需要使用枚举
- 介绍
- 继承自java.lang.Enum,枚举实质上还是类。
- 默认都是 pubilc static final。
代码演示
| public class TestEnum { |
| public static void main(String[] args) { |
| |
| System.out.println(Season.SPRING); |
| |
| |
| Season autumn = Season.AUTUMN; |
| switch (autumn){ |
| case SPRING: |
| System.out.println("春天"); |
| break; |
| case SUMMER: |
| System.out.println("夏天"); |
| break; |
| case AUTUMN: |
| System.out.println("秋天"); |
| break; |
| case WINTER: |
| System.out.println("冬天"); |
| break; |
| default: |
| System.out.println("储存"); |
| return; |
| } |
| } |
| } |
| |
| enum Season{ |
| SPRING,SUMMER,AUTUMN,WINTER |
| } |
| |
| enum Week{ |
| 星期一,星期二,星期三,星期四,星期五,星期六,星期日 |
| } |
Jar包中展示
| |
| public enum Propagation { |
| REQUIRED(0), |
| SUPPORTS(1), |
| MANDATORY(2), |
| REQUIRES_NEW(3), |
| NOT_SUPPORTED(4), |
| NEVER(5), |
| NESTED(6); |
| |
| private final int value; |
| |
| private Propagation(int value) { |
| this.value = value; |
| } |
| |
| public int value() { |
| return this.value; |
| } |
| } |
枚举中的 Code Msg Flag测试
| public enum GbRespCode { |
| R_OK(0, "请求成功"), |
| R_1(-1, "系统繁忙,请稍后重试"), |
| R_4001(4001, "签名错误"), |
| R_4002(4002, "Token错误"), |
| R_4003(4003, "Post参数不合法,缺少必需的示例:OperatorID,Sig,TimeStamp,Data,Seq五个参数"), |
| R_4004(4004, "请求的业务参数不合法"), |
| R_4005(4005, "未查询到该订单"), |
| R_4006(4006, "查询充电站信息错误"), |
| R_4007(4007, "设备状态变化推送失败"), |
| R_4008(4008, "设备未授权"), |
| R_4009(4009, "站点未授权"), |
| R_4010(4010, "查询充电站状态错误"), |
| R_4011(4011, "推送充电状态失败"), |
| R_4012(4012, "推送充电订单信息失败"), |
| R_4013(4013, "设备接口编码不符合规范"), |
| R_4014(4014, "未查询到该桩"), |
| R_4015(4015, "未查询到该场站"), |
| R_4016(4016, "入参设备ID列表不能为空"), |
| R_4017(4017, "外部订单号只能为27位字符串"), |
| R_4018(4018, "开始时间应小于结束时间"), |
| R_4019(4019, "时间相差不能超过一天"), |
| R_500(500, "系统错误"); |
| |
| private String msg; |
| private int code; |
| |
| private GbRespCode(int code, String msg) { |
| this.code = code; |
| this.msg = msg; |
| } |
| |
| public String getMsg() { |
| return this.msg; |
| } |
| |
| public int getCode() { |
| return this.code; |
| } |
| } |
| |
| |
| System.out.println(GbRespCode.R_500.getCode() + ":" + GbRespCode.R_500.getMsg()); |
| |
| |
| |
| System.out.println(GbRespCode.R_500); |
通过bizCode获取bizDesc值
- 枚举类
| public enum DictionaryEnum { |
| ADJUST_GAIN("ADJUST", "GAIN", "库存调整-报溢"), |
| ADJUST_LOSS("ADJUST", "LOSS", "库存调整-报损"); |
| |
| private String parentCode; |
| private String bizCode; |
| private String bizDesc; |
| |
| private DictionaryEnum(String parentCode, String bizCode, String bizDesc) { |
| this.parentCode = parentCode; |
| this.bizCode = bizCode; |
| this.bizDesc = bizDesc; |
| } |
| |
| public String getParentCode() { |
| return this.parentCode; |
| } |
| |
| public String getBizCode() { |
| return this.bizCode; |
| } |
| |
| public String getBizDesc() { |
| return this.bizDesc; |
| } |
| |
| public static DictionaryEnum enumOfBizCode(String bizCode) { |
| DictionaryEnum[] var1 = values(); |
| int var2 = var1.length; |
| |
| for (int var3 = 0; var3 < var2; ++var3) { |
| DictionaryEnum type = var1[var3]; |
| if (type.getBizCode().equals(bizCode)) { |
| return type; |
| } |
| } |
| throw new IllegalArgumentException("Cannot find enum with value " + bizCode); |
| } |
| } |
- 测试类
| String GAIN = "GAIN"; |
| String LOSS = "LOSS"; |
| |
| |
| System.out.println(DictionaryEnum.enumOfBizCode(GAIN).getBizDesc()); |
| |
| System.out.println(DictionaryEnum.enumOfBizCode(LOSS).getBizDesc()); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 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的设计差异
· 三行代码完成国际化适配,妙~啊~