[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
把常量定义在接口里与类里都能通过编译,那2者到底有什么区别呢?
那个更合理?
常量接口
public interface ConstInterfaceA { public static final String CONST_A = "aa";
public static final String CONST_C = "cc"; }
存在问题:
* 无法限制开发人员, 继承/实现接口
* 开发人员能够在子接口里继续添加常量,而这些常量可能得不到祖先层的支持
* 常量作为参数时, 是 String, int 等弱类型, 开发人员可以传入没有在常量接口里定义的值, 这个问题无法通过编译器发现
* 由于开发人员可以直接写常量值, 所以不能用 == 对比, 只能用 equals 对比, 不能优化性能
* 开发人员在没有参考资料时, 不可能知道某个 int 型的参数到底应该赋什么内容
* 编译时, 是直接把常量的值编译到类的二进制代码里, 常量的值在升级中变化后, 需要重新编译所有引用常量的类, 因为里面存的是旧值
常量类
public class ConstClassA { public static final String CONST_A = "aa"; public
static final String CONST_C = "cc"; private ConstClassA() { } }
* 常量类可以设置构造函数为 private
* 从而限制继承, 也就没有继续添加常量的问题了
* 但是其他问题与常量接口一样无法解决
枚举常量类
public class EnumClassA { private String name; private EnumClassA(String name)
{ this.name = name; } public static final EnumClassA CONST_A = new
EnumClassA("aa"); public static final EnumClassA CONST_C = new
EnumClassA("cc"); }
解决了以上所有问题, 主要体现在:
* 私有构造函数, 避免被继承和扩展
* 定义方法的参数时, 必须用枚举常量类类型, 如上面的 EnumClassA 类型, 这样就转变成了强类型, 不会出现弱类型引起的问题
* 常量值地址唯一, 可以用 == 直接对比, 性能会有提高
* 开发人员可以根据该参数类型打开对应的类, 从而找到定义的常量
* 编译时, 没有把常量值编译到代码里, 即使常量的值发生变化也不会影响引用常量的类
enum 类型
public static enum Grade { A(4), B(3), C(2), D(1), F(0); private int points;
Grade(int points) { this.points = points; } int getPoints() { return points; }
};
* 这是 JDK 1.5 引入的, 其实就是枚举常量类的代码封装简化而已
* 查看 enum 反编译后的代码与枚举常量类的结构非常相似
* 这可能是因为 java 的设计者一开始觉得 enum 与 OO 思想不符
* 所以没有提供支持, 但是随着常量接口的滥用和枚举常量类方案的出现
* 才在 JDK 1.5 里增加了 enum