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
package Learn;
public class Test {
public static void main(String[] args) {
System.out.println(Season.SPRING.toString());
System.out.println(Season.SUMMER.toString());
System.out.println(Season.AUTUMN.toString());
System.out.println(Season.WINTER.toString());
}
}
class Season{
private String season, characteristic;
/**将构造其私有化,防止在main方法中直接new对象*/
private Season(String season,String characteristic){
this.season=season;
this.characteristic=characteristic;
}
/**定义四个季节的特点,使用static使得在main方法中不用创建实例对象,但是会造成类的加载,所以再加上final变成常量值,避免了类的加载*/
public static final Season SPRING =new Season("春天","温暖");
public static final Season SUMMER =new Season("夏天","炎热");
public static final Season AUTUMN =new Season("秋天","凉爽");
public static final Season WINTER =new Season("冬天","寒冷");

@Override
public String toString(){
return season+characteristic;
}

}

image-20220322084509210

  1. 不需要提供set方法,因为枚举对象值通常为只读。
  2. 对枚举对象属性使用final+static共同修饰,实现底层优化
  3. 枚举对象属于常量,通常全部大写

enum关键字枚举

将定义的常量对象定义在最前面

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
package Learn;
public class Test {
public static void main(String[] args) {
System.out.println(Season.SPRING.toString());
System.out.println(Season.SUMMER.toString());
System.out.println(Season.AUTUMN.toString());
System.out.println(Season.WINTER.toString());
}
}
enum Season{
SPRING("春天","温暖"),
SUMMER ("夏天","炎热"),
AUTUMN ("秋天","凉爽"),
WINTER ("冬天","寒冷");
private String season, characteristic;
/**将构造其私有化,防止在main方法中直接new对象*/
private Season(String season,String characteristic){
this.season=season;
this.characteristic=characteristic;
}


@Override
public String toString(){
return season+characteristic;
}

}

image-20220322084543249

注意事项:

  • 当我们使用enum关键字时,默认会继承enum类,我们反编译一下文件,可以发现同时也是final类型点击并拖拽以移动image-20220322084630144

  • 如果使用无参构造器,则实参列表与小括号都可以省略。例如下面的OTHER

image-20220322084718169

  • img点击并拖拽以移动这个也是正确的,默认调用的无参构造器。

枚举中常用的方法

上面我们说到,enum其实是继承了ENUM类的,所以也会继承其方法

方法名 作用
name 输出对象的名字
ordinal 输出该对象在枚举中的位置(从0开始)
hascode 输出对应的对象地址编号
value 将枚举填入数组中
valueof 到对应的对象中去寻找,如果找到了,返回该对象,否则为false
compareTo 比较两个对象的枚举编号,是他们顺序号的差值,可以crtl+B查看源码
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
package Learn;
public class Test {
public static void main(String[] args) {
System.out.println(Season.SPRING.toString());
System.out.println(Season.SUMMER.toString());
System.out.println(Season.AUTUMN.toString());
System.out.println(Season.WINTER.toString());
System.out.println(Season.SPRING.name());
System.out.println(Season.SPRING.hashCode());
//输出相应的编号,编号从0开始
System.out.println(Season.SPRING.ordinal());
Season []values=Season.values();
for (Season A :values){
System.out.println(A);
}
System.out.println(Season.valueOf("SPRING"));
//System.out.println(Season.valueOf("dj"));
System.out.println(Season.SPRING.compareTo(Season.AUTUMN));

}
}
enum Season{
SPRING("春天","温暖"),
SUMMER ("夏天","炎热"),
AUTUMN ("秋天","凉爽"),
WINTER ("冬天","寒冷"),
OTHER;
private String season, characteristic;
/**将构造其私有化,防止在main方法中直接new对象*/
private Season(){}
private Season(String season,String characteristic){
this.season=season;
this.characteristic=characteristic;
}


@Override
public String toString(){
return season+characteristic;
}

}

image-20220322084756180

练习:输出每一周的星期

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
package Learn;

public class Exercise {
public static void main(String[] args) {
Week [] weeks= Week.values();
for (Week week :weeks){
System.out.println(week.toString());
}

}
}
enum Week{
Monday("星期一"),
Tuesday("星期二"),
Wednesday("星期三"),
Thursday("星期四"),
Friday("星期五"),
Saturday("星期六"),
Sunday("星期日");

private String week;
Week(String week){
this.week=week;
}

@Override
public String toString() {
return week ;
}
}

image-20220322084840211

注意:

如果我们使用enum关键字之后,就不可以继承其他类了,Java是单继承机制。但是实质上enum还是一个类,所以可以继承实现接口。