探究枚举与接口常量、类常量的区别

作者:小牛呼噜噜 | https://xiaoniuhululu.com
计算机内功、JAVA底层、面试相关资料等更多精彩文章在公众号「小牛呼噜噜 」

一个简单的需求

在我们实际开发java项目过程中,突然有一天”领导老王”给了个任务, 公司系统需要支持商品管理的需求
比如水果有:苹果,香蕉,葡萄等等,电子产品有:电脑,手机,摄像机等等

我们一般新建商品类Goods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
}
}

然后我们就直接可以使用它:

1
2
3
4
5
6
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代表什么意思,而且在业务代码层里面直接写数字或者字符串也是非常危险的时,我们需要一种方案,既能将相关的状态,类型放在一起,又可以限制类的输入值,提升项目的安全性

接口常量

我们可以使用接口常量来解决上面的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}

我们再来看下测试类:

1
2
3
4
5
6
7
8
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中接口是支持多继承的

  • 我们可以将内容深入到其实现类代码中,这样对于一个常量类接口来说显然是不合理。
  • 我们还可以在其子接口里继续添加常量,这样在祖先接口中就无法控制所有常量,这样无疑是非常危险的。

一般不建议用的,但接口常量也不是一无是处的,可以通过内部接口来实现分组效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}

}

这样我们可以把相关的常量都归为一类,更加简洁明了

类常量

我们一般常用的是类常量方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 化,防止被实例化

测试类:

1
2
3
4
5
6
7
8
public class GoodsTest3 {
public static void main(String[] args) throws InterruptedException {