一、枚举类的使用:
1.枚举类的理解:类的对象只有有限个、确定的;我们称此类为枚举类;
2.需要定义一组常量时,强烈建议使用枚举类;
3.如果枚举类中只有一个对象,可以作为单例模式的一种实现方式;
二、如何定义枚举类:
方式一、jdk5.0之前,自定义枚举类
1 @Test 2 public void testEume() { 3 Season spring = Season.SPRING; 4 System.out.println(spring.toString()); 5 6 7 }
1 //自定义枚举类 2 class Season{ 3 private final String seasonName; 4 private final String seasonDesc; 5 //1.私有化构造器 6 private Season(String seasonName,String seasonDesc) { 7 this.seasonName = seasonName; 8 this.seasonDesc = seasonDesc; 9 } 10 11 public String getSeasonName() { 12 return seasonName; 13 } 14 15 public String getSeasonDesc() { 16 return seasonDesc; 17 } 18 19 20 public static final Season SPRING = new Season("spring","春天"); 21 public static final Season SUMMER = new Season("summer","夏天"); 22 public static final Season AUTOUM = new Season("autumn","秋天"); 23 public static final Season WINTER = new Season("winter","冬天"); 24 @Override 25 public String toString() { 26 return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]"; 27 } 28 29 30 }
System.out.println(spring.toString());打印结果:
Season [seasonName=spring, seasonDesc=春天]
方式二、5.0可以使用enum关键字定义枚举类
1 //使用enum
2 //定义的枚举类继承与 java.lang.Enum
3 enum Season1{
4 //1.提供枚举类的对象,多个对象之间用逗号隔开;
5 SPRING("spring","春天"),
6 SUMMER("summer","夏天"),
7 AUTOUM("autumn","秋天"),
8 WINTER("winter","冬天");
9 //2.声明Season对象的属性,private final修饰;
10 private final String seasonName;
11 private final String seasonDesc;
12
13
14 //3.私有化构造器,并且给对象赋值
15 private Season1(String seasonName,String seasonDesc) {
16 this.seasonName = seasonName;
17 this.seasonDesc = seasonDesc;
18 }
19 //其他诉求
20 public String getSeasonName() {
21 return seasonName;
22 }
23
24 public String getSeasonDesc() {
25 return seasonDesc;
26 }
27
28 }
三、enum的常用方法:
1.values():返回枚举类型的对象数组,该方法可以很方便的遍历所有枚举值;
2.valueOf(String str):可以把一个字符串转为对应的枚举类对象,要求字符串必须是枚举对象的名字
3.toString(),返回当前枚举类对象常量的名称;
使用枚举类:
1 @Test
2 public void testEume() {
3 Season1 spring1 = Season1.SPRING;
4 System.out.println(spring1.toString());
5 Season1[] values = Season1.values();
6 for(int i=0;i<values.length;i++) {
7 System.out.println(values[i].toString());
8 }
9 }
测试结果:
SPRING
SPRING
SUMMER
AUTOUM
WINTER
四、使用enum关键字定义的枚举类实现接口的情况;
1.情况1.实现接口;在enum中使用抽象方法;
2.情况2.每个对象实现接口;
代码如下;
1 //使用enum
2 //定义的枚举类继承与 java.lang.Enum
3 enum Season1 implements info{
4 //1.提供枚举类的对象,多个对象之间用逗号隔开;
5 SPRING("spring","春天"){
6 public void show() {
7 // TODO Auto-generated method stub
8 System.out.println("春天在哪里");
9
10 }
11 },
12 SUMMER("summer","夏天"){
13 public void show() {
14 // TODO Auto-generated method stub
15 System.out.println("宁夏");
16 }
17 },
18 AUTOUM("autumn","秋天"){
19 public void show() {
20 // TODO Auto-generated method stub
21 System.out.println("秋天不回来");
22 }
23 },
24 WINTER("winter","冬天"){
25 public void show() {
26 // TODO Auto-generated method stub
27 System.out.println("大约在冬季");
28 }
29 };
30 //2.声明Season对象的属性,private final修饰;
31 private final String seasonName;
32 private final String seasonDesc;
33
34
35 //3.私有化构造器,并且给对象赋值
36 private Season1(String seasonName,String seasonDesc) {
37 this.seasonName = seasonName;
38 this.seasonDesc = seasonDesc;
39 }
40 //其他诉求
41 public String getSeasonName() {
42 return seasonName;
43 }
44
45 public String getSeasonDesc() {
46 return seasonDesc;
47 }
48
49
50 @Override
51 public String toString() {
52 return "Season1 [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
53 }
54 }
1 interface info{
2 void show();
3 }
1 @Test
2 public void testEume() {
3
4 Season1 spring1 = Season1.SPRING;
5 System.out.println(spring1.toString());
6 Season1[] values = Season1.values();
7 for(int i=0;i<values.length;i++) {
8 System.out.println(values[i].toString());
9 }
10 //valueof(String objName):返回枚举类中对象名是objName的对象;如果没有objName找到,则抛出异常
11 Season1 spring2 = Season1.valueOf("SPRING");
12 System.out.println(spring2.toString());
13 spring2.show();
14
15
16 }
测试结果:
Season1 [seasonName=spring, seasonDesc=春天]
Season1 [seasonName=spring, seasonDesc=春天]
Season1 [seasonName=summer, seasonDesc=夏天]
Season1 [seasonName=autumn, seasonDesc=秋天]
Season1 [seasonName=winter, seasonDesc=冬天]
Season1 [seasonName=spring, seasonDesc=春天]
春天在哪里