作者:小牛呼噜噜 | https://xiaoniuhululu.com
计算机内功、JAVA底层、面试相关资料等更多精彩文章在公众号「小牛呼噜噜 」
在我们实际开发java项目过程中,突然有一天"领导老王"给了个任务, 公司系统需要支持商品管理的需求
比如水果有:苹果,香蕉,葡萄等等,电子产品有:电脑,手机,摄像机等等
我们一般新建商品类Goods:
public class Goods {
/**
* 商品名称
*/
private String name;
/**
* 商品类型
*/
private Integer type;
public Goods(String name, Integer type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
然后我们就直接可以使用它:
public class GoodsTest {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods("水果",1);//1代表苹果,2:香蕉,3:葡萄
System.out.println(goods.getName());
}
}
但是有个问题,业务代码不清晰,有时候开发人员并不知道1、2、3代表什么意思,而且在业务代码层里面直接写数字或者字符串也是非常危险的时,我们需要一种方案,既能将相关的状态,类型放在一起,又可以限制类的输入值,提升项目的安全性
我们可以使用接口常量来解决上面的问题
public interface StatusContentFace {
public static final String fruit = "fruit";
public static final Integer apple = 1;
public static final Integer banana = 2;
public static final Integer grape = 3;
//==========================
public static final String eleProduct = "eleProduct";
public static final Integer computer = 101;
public static final Integer phone = 102;
public static final Integer camera = 103;
}
我们再来看下测试类:
public class GoodsTest1 {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods(StatusContentFace.fruit,StatusContentFace.apple);
Goods goods_2 = new Goods(StatusContentFace.eleProduct,StatusContentFace.computer);
System.out.println(goods.getName());
System.out.println(goods_2.getName());
}
}
这样能够让相关的常量都在同一个接口文件中,接口常量,写起来比较简洁,但是为了让其他人知道每个常量的含义,最好写上注释。
但它同时有个问题,由于java中接口是支持多继承的
一般不建议用的,但接口常量也不是一无是处的,可以通过内部接口来实现分组效果
public class GoodsTest2 {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods(Fruit.type,Fruit.banana);
Goods goods_2 = new Goods(EleProduct.type,EleProduct.phone);
System.out.println(goods.getName());
System.out.println(goods_2.getName());
}
//常量分组
public interface Fruit {
String type = "fruit";
Integer apple = 1;
Integer banana = 2;
Integer grape = 3;
}
public interface EleProduct {
String type = "eleProduct";
Integer computer = 101;
Integer phone = 102;
Integer camera = 103;
}
}
这样我们可以把相关的常量都归为一类,更加简洁明了
我们一般常用的是类常量方式:
public final class StatusConstant {
private StatusConstant() {} //防止该类实例化
public static final String fruit = "fruit";
public static final Integer apple = 1;
public static final Integer banana = 2;
public static final Integer grape = 3;
//==========================
public static final String eleProduct = "eleProduct";
public static final Integer computer = 101;
public static final Integer phone = 102;
public static final Integer camera = 103;
}
注意:一般用final关键字修饰 class 防止其被继承,并将其构造函数 private 化,防止被实例化
测试类:
public class GoodsTest3 {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods(StatusConstant.fruit, StatusConstant.banana);
Goods goods_2 = new Goods(StatusConstant.eleProduct, StatusConstant.phone);
System.out.println(goods.getName());
System.out.println(goods_2.getName());
}
}
我们可以发现类常量的方式,的确很方便,也没有接口常量多继承的烦恼。但是她所能承接的信息,维度不够,只能一个字段的去承接信息,然而当项目复杂的话,我们希望往往其能承接更多维度的信息,类似于对象一样,拥有更多的属性
{
"name": ...,
"type": ...,
...
}
这时候,我们本文的主角,枚举就闪亮登场了!

枚举是一种特殊的类,所有的枚举类都是Enum类的子类,就类似Object类一样,由于java类是单继承的,所以不能在继承其他类或者枚举了。
枚举变量不能使用其他的数据,只能使用枚举中常量赋值。能提高程序的安全性。
格式:
public enum 枚举名{
//枚举的取值范围
}
我们先定义一个枚举类,来定义常量:
public enum ContentEnums {
Apple(1,"苹果"),
Banana(2,"香蕉"),
Grape(3,"葡萄"),
Computer(101,"电脑"),
Phone(102,"手机"),
Camera(103,"摄像机"),
Fruit(10010,"fruit"),
EleProduct(10020,"eleProduct");
private Integer code;
private String desc;
ContentEnums(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
测试类:
public class GoodsTest4 {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods(ContentEnums.Fruit.getDesc(), ContentEnums.Apple.getCode());
Goods goods_2 = new Goods(ContentEnums.EleProduct.getDesc(), ContentEnums.Phone.getCode());
System.out.println(goods.getName());
System.out.println(goods_2.getName());
}
}
看到这大家可能就有疑问了,枚举和常量类相比,有什么优点吗?
if/elseContentEnums content = ContentEnums.Apple;
switch (content) {
case Apple:
System.out.println("苹果");
break;
case Banana:
System.out.println("香蕉");
break;
case Grape:
System.out.println("葡萄");
break;
default:
System.out.println("未找到匹配类型");
}
public enum MethodEnums {
VERSION {
@Override
String getInfo() {
return System.getProperty("java.version");
}
},
DATE_TIME {
@Override
String getInfo() {
return
DateFormat.getDateInstance()
.format(new Date());
}
};
abstract String getInfo();
public static void main(String[] args) {
for(MethodEnums csm : values()) {
System.out.println(csm.getInfo());
}
}
}
结果:
1.8.0_271
2022-9-21
除了抽象方法,普通方法也是可以的,这里就不展示了
我们可以通过枚举来将相关的状态,类型放在一起,文章一开头,但我们怎么才能限制类的输入值呢?其实很简单,别被绕进去,我们只需将输入类型 改为指定的枚举即可
我们改造一下Goods类:
public class Goods {
/**
* 商品名称
*/
private String name;
/**
* 商品类型
*/
private Integer type;
// public Goods(String name, Integer type) {
// this.name = name;
// this.type = type;
// }
public Goods() {//防止外部实例化
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public static Goods addGoods(ContentEnums enums){
Goods goods = new Goods();
goods.setName(enums.getDesc());
goods.setType(enums.getCode());
return goods;
}
}
测试类:
public class GoodsTest5 {
public static void main(String[] args) throws InterruptedException {
Goods goods = Goods.addGoods(ContentEnums.Apple);
Goods goods_2 = Goods.addGoods(ContentEnums.Computer);
System.out.println(goods.getName());
System.out.println(goods_2.getName());
}
}
这样,我们就可以限制创建对象时的输入值类型了
可以使用==来比较 enum 实例,编译器会自动为你提供equals()和 hashCode() 方法。Enum 类实现了 Comparable 接口,所以它具有 compareTo() 方法。同时,它还实现了 Serializable 接口。
枚举类型是天生线程安全的,并且只会装载一次,我们可以利用了枚举的这个特性来实现单例
public enum SingleInstance {
INSTANCE;
public void funDo() {
System.out.println("doSomething");
}
}
使用方式:SingleInstance.INSTANCE.funDo()
这种方法充分 利用枚举的特性,让JVM来帮我们保证线程安全和单一实例的问题。写法也极其简洁。
参考:
《On Java 8》
《Effective java》第3版
本篇文章到这里就结束啦,很感谢你能看到最后,如果觉得文章对你有帮助,别忘记关注我!更多精彩的文章

类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象