枚举类

abstract

在数据交互时,常量往往和显示到页面的并不一样,如表示性别时,男(0),女(1)。代码里记 0、1 页面表示用男女。

1
2
3
4
int gender = xxx;
if(gender == 1){
//TODO
}

在类型判断的时候直接使用01==equals并不利于代码的强壮性。

可以有两种解决方案:

  1. 自定义常量类或常量接口
  2. 枚举类

常量类

可以用类或接口专门存放常量值

IWeekdayConstants.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
25
26
27
28
29
30
31
32
33
34
35
public interface IWeekdayConstants{
public static final int SUN = 0;
public static final String SUN_CAPTION = "星期一";

public static final int MON = 1;
public static final String MON_CAPTION = "星期二";

public static final int TUE = 2;
public static final String TUE_CAPTION = "星期三";

public static final int WED = 3;
public static final String WED_CAPTION = "星期四";

public static final int THU = 4;
public static final String THU_CAPTION = "星期五";

public static final int FRI = 5;
public static final String FRI_CAPTION = "星期六";

public static final int SAT = 6;
public static final String SAT_CAPTION = "星期天";

public static final Map<Integer, String> WEEKDAY_MAP = new LinkedHashMap<Integer, String>(){
private static final long serialVersionUID = 123L;
{
this.put(SUN,SUN_CAPTION);
this.put(MON,MON_CAPTION);
this.put(TUE,TUE_CAPTION);
this.put(WED,WED_CAPTION);
this.put(THU,THU_CAPTION);
this.put(FRI,FRI_CAPTION);
this.put(SAT,SAT_CAPTION);
}
};
}

Test.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Test {
public static void main(String[] args) throws Exception{

int day = xxx;
//比较
if(IWeekdayConstants.SUN == day || IWeekdayConstants.SAT == day){
System.out.println("睡到自然醒");
}
//遍历
for(Map.Entry<Integer, String> entry:IWeekdayConstants.WEEKDAY_MAP.entrySet){
System.out.println(entry.getKey() +":" + entry.getValue());
}
//switch
switch(day){
case IWeekdayConstants.SUN:
//TODO
break;
case IWeekdayConstants.MON:
//TODO
break;
case IWeekdayConstants.TUE:
//TODO
break;
case IWeekdayConstants.WED:
//TODO
break;
case IWeekdayConstants.THU:
//TODO
break;
case IWeekdayConstants.FRI:
//TODO
break;
case IWeekdayConstants.SAT:
//TODO
break;
default :
throw new RunTimeException("一周七天够用了");
}
}
}

LindedHashMap中必须用到serialVersionUID,如果没有serialVersionUID字段,则对类的任何更改都将使以前序列化的版本不可读。[注1]

这种方式实现枚举有些问题,就是无法验证需要对比的变量是否合理。如:int day = 7

或者仍可以作为其他用途,如:

1
2
3
4
int man = 1;
if(man == IWeekday.MON){
//TODO
}

编译通过,但用枚举用途就混乱了。

枚举类

可以通过枚举类型实现上述所有功能,而且功能专用。

1
2
3
public enum Weekday {
SUN,MON,TUE,WED,THU,FRI,SAT;
}

简单的一行代码实现了之前50的功能,能验证合理性,也用途专一,因为类型不同。

Weekday.MON的类型是Weekday.

特性

enum定义的类型就是class,enum类编辑的class类似于这样:

1
2
3
4
5
6
7
8
9
10
public final class Weekday extends Enum { 
public static final Weekday SUN = new Weekday();
public static final Weekday MON = new Weekday();
public static final Weekday TUE = new Weekday();
public static final Weekday WED = new Weekday();
public static final Weekday THU = new Weekday();
public static final Weekday FRI = new Weekday();
public static final Weekday SAT = new Weekday();
private Weekday() {}
}
  1. 隐藏构造方法,使得每个实例都是唯一的。
  2. 定义的enum类型总是继承自java.lang.Enum,且无法被继承;
  3. 只能定义出enum的实例,而无法通过new操作符创建enum的实例;
  4. 可以将enum类型用于switch语句。

枚举类是一种引用类型,引用类型需要使用equals来进行比较,如果使用==则比较的是两个对象是否相等。

但枚举类是一个特殊的引用类型,因为在虚拟机内存中只存在唯一实例,所以使用==比较也是可以的。

方法

name()

1
String name = Weekday.MON.name();//"MON"

ordinal()

1
int num = Weekday.MON.ordinal();//1

这个顺序根据常量在类中的顺序,类似于LinkedMap;

如果使用这个方法做逻辑,之后类中常量的顺序又改变了,倒是程序出问题,强壮性很低。

扩展枚举类

但可以稍微改造下枚举类

1
2
3
4
5
6
7
8
9
public enum Weekday {
MON(1),TUE(2),WED(3),THU(4),FRI(5),SAT(6),SUN(0);

public final int daySort;

private Weekday(int daySort){
this.daySort = daySort;
};
}

既然可以加逻辑顺序,那岂不是还可以加别的信息呢?

1
2
3
4
5
6
7
8
9
10
11
public enum Weekday {
MON(1, "星期一"), TUE(2, "星期二"), WED(3, "星期三"), THU(4, "星期四"), FRI(5, "星期五"), SAT(6, "星期六"), SUN(0, "星期日");

public final int daySort;
public final String caption;

private Weekday(int daySort,String caption){
this.daySort = daySort;
this.caption = caption;
};
}

默认的toString()和name()返回结果是一样的,但是添加逻辑属性之后你就可以通过重写toString()来返回想要的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public enum Weekday {
MON(1, "星期一"), TUE(2, "星期二"), WED(3, "星期三"), THU(4, "星期四"), FRI(5, "星期五"), SAT(6, "星期六"), SUN(0, "星期日");

private final int daySort;
private final String caption;

private Weekday(int daySort,String caption){
this.daySort = daySort;
this.caption = caption;
};
public int getDaySort(){
return daySort;
}
public String getCaption(){
return caption;
}
@OverWrite
public String toString(){
return this.caption;
};

pub
}

switch

switch的用法和上面接口的实现方式一样,最后需要加上default放值漏写某一枚举常量;

Test.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Test {
public static void main(String[] args) throws Exception{

Weekday day = Weekday.MON;
//比较
if(Weekday.SUN == day || Weekday.SAT == day){
System.out.println("睡到自然醒");
}
//遍历
//通过枚举类的静态方法
for (Weekday day:Weekday.values()){
System.out.println(day.getDaySort() + ":" + day.toString());
}
//通过反射
Class<Weekday> clz = Weekday.class;
for (Weekday day:clz.getEnumConstants()){
System.out.println(day.getDaySort() + ":" + day.toString());
}
//switch
switch(day){
case Weekday.SUN:
//TODO
break;
case Weekday.MON:
//TODO
break;
case Weekday.TUE:
//TODO
break;
case Weekday.WED:
//TODO
break;
case Weekday.THU:
//TODO
break;
case Weekday.FRI:
//TODO
break;
case Weekday.SAT:
//TODO
break;
default :
throw new RunTimeException("一周七天够用了");
}
}
}