接口 VS 枚举,如何管理常量?

你平时有哪种方式,管理常量呢?

  • public static final关键字
  • 接口:管理状态码或配置选项,需要在不同类中共享
  • 枚举:管理有限的常量集合,具有相关的信息提示或描述

1.public static final关键字?

public class Demo{
   
  public static fianl String RESULT_YES = "1";
  public static fianl String RESULT_NO = "0";
}

// 实例
public static class DemoServiceImpl{
   

  public static void main(String []arg){
   
    
    System.out.println(Demo.RESULT_YES);  // 执行结果:1
    
    System.out.println(Demo.RESULT_NO);   // 执行结果:0
  
  }
  
}

2.接口?

public class Demo{
   
  // public static fianl String RESULT_YES = "1";
  // public static fianl String RESULT_NO = "0";
  
  // 存储上报内容
  public interface Uplink{
   
    String HEARTBEAT = "01";
    String FLOW = "03";
  }
  
  // 存储反馈的内容
  public interface Downward{
   
    String HEARTBEAT_BACK = "02";
    String FLOW_BACK = "04";
  }
  
}


// 实例
public static class DemoServiceImpl{
   

  public static void main(String []arg){
   
  
    System.out.println(Demo.Uplink.HEARTBEAT);  // 执行结果:01
    System.out.println(Demo.Uplink.FLOW);   // 执行结果:03
  
    System.out.println(Demo.Downward.HEARTBEAT_BACK);  // 执行结果:02
    System.out.println(Demo.Downward.FLOW_BACK);   // 执行结果:04
  
  }
  
}

3.枚举?


public class Demo{
   
  public static fianl String RESULT_YES = "1";
  public static fianl String RESULT_NO = "0";
  
  public enum demo{
   
        YES(200, "执行成功"), ERROR_500(500, "程序内部执行错误"), ERROR_404(404, "请求超时");

        private final int code;
        private final String desc;

        demo(int code, String desc){
   
            this.code = code;
            this.desc = desc;
        }

        public int getCode(){
   
            return code;
        }

        public String getDesc(){
   
            return desc;
        }
    }
  
}


// 实例
public static class DemoServiceImpl{
   

  public static void main(String []arg){
   
    
    int code = demo.YES.getCode();
    System.out.println(code);
    
    String desc = demo.YES.getDesc();
    System.out.println(desc);
  }
  
}

这三种方法的使用场景是什么?

  1. public static final 是最常用的常量表示方式,可以用在任意场合;

  2. 接口:定义的常量,可供多个类共享;比如,我们可以通过该方法,实现对tcp协议上行/下行数据,进行分类管理;

  3. 枚举:像错误码,这种需要内容描述的,使用枚举实现;例如,错误类型500/ 404/ 200/ 503…(详见上文例子)

相关推荐

  1. 接口 VS 如何管理常量

    2023-12-26 16:24:02       31 阅读
  2. golang接口//结构使用示例

    2023-12-26 16:24:02       10 阅读
  3. 【go从入门到精通】常量详解

    2023-12-26 16:24:02       13 阅读
  4. 设计模式:如何实现单例模式

    2023-12-26 16:24:02       13 阅读
  5. 折半(题目)

    2023-12-26 16:24:02       48 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-26 16:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-26 16:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-26 16:24:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-26 16:24:02       20 阅读

热门阅读

  1. 6-2 递归求阶乘和

    2023-12-26 16:24:02       37 阅读
  2. BindingData涉及的三个关键元素:数据源 路径 目标

    2023-12-26 16:24:02       36 阅读
  3. 解析新时代AI------在安全与发展之间寻求平衡

    2023-12-26 16:24:02       38 阅读
  4. PCBA贴片加工厂不良原因分析

    2023-12-26 16:24:02       41 阅读
  5. 面试算法71:按权重生成随机数

    2023-12-26 16:24:02       37 阅读
  6. oracle11体系结构二-存储结构

    2023-12-26 16:24:02       31 阅读
  7. joiner

    2023-12-26 16:24:02       42 阅读
  8. 接口合集:含各种免费好用的api

    2023-12-26 16:24:02       39 阅读