zoukankan      html  css  js  c++  java
  • 枚举

    枚举的基本用法

    时间:2020年10月22日09:57:40

    1. 老鸟建议
      • 不要使用枚举的高级特性,如果当你都用高级特性的时候,那就不如把这个类定义成为一个普通类。不要那么麻烦了。
      • 当你需要一组常量的时候,可以使用枚举类型。
      • 枚举是用来方便的,不是用来搞特殊的。
    2. 什么时候需要使用枚举
      • 需要定义一组常量的时候。
    3. 介绍
      • 继承自java.lang.Enum,枚举实质上还是
      • 默认都是 pubilc static final。

    代码演示

    public class TestEnum {
        public static void main(String[] args) {
            //直接输出:春天
            System.out.println(Season.SPRING);
            
            //测试跟 switch 语句结合起来
            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包中展示

    //@Transactional(propagation = Propagation.REQUIRED) 枚举常量序数:0
    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;
        }
    }
    // 测试枚举
    // TODO 输出:500:系统错误
    System.out.println(GbRespCode.R_500.getCode() + ":" + GbRespCode.R_500.getMsg());
    
    // 测试枚举
    // TODO 输出:R_500
    System.out.println(GbRespCode.R_500);
    

    通过bizCode获取bizDesc值

    1. 枚举类
    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);
        }
    }
    
    1. 测试类
    String GAIN = "GAIN";
    String LOSS = "LOSS";
    
    // 库存调整-报溢
    System.out.println(DictionaryEnum.enumOfBizCode(GAIN).getBizDesc());
    // 库存调整-报损
    System.out.println(DictionaryEnum.enumOfBizCode(LOSS).getBizDesc());
    
  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/Twittery/p/14384177.html
Copyright © 2011-2022 走看看