关键字: enum
Enum是enumeration(列举)的简写形式,包含在java.lang包中.
Java代码
1. public enum Season { winter, spring, summer, fall }
一个enum是定义一组值的对象,它可以包括零个或多个值成员.它是属于enum类型的,一个enum对象中不可有两个或多个相同的属性或值.在次之前的java程序员一般是 用接口的方法实现列举的,
Java代码
1. public interface Season {
2.
3. static winter = 0;
4.
5. static spring = 1; //etc..
6.
7. }
引入了enum的java的列举的编写方便了许多,只须定义一个enum型的对象.enum对象的值都回自动获得一个数字值,从0开始,依次递增.看一个比较简单的enum实现的例子:
Java代码
1. //EnumDemo.java
2.
3. /**
4. * We can loop over the values we put into the enum using the values() method.
5. * Note that the enum Seasons is compiled into a separate unit, called
6. * EnumDemo$Seasons.class title:
7. */
8. public class EnumDemo {
9.
10. /*
11. * declare the enum and add values to it. note that, like in C#, we don't
12. * use a ; to end this statement and we use commas to separate the values
13. */
14.
15. private enum Seasons {
16. winter, spring, summer, fall
17. }
18.
19. // list the values
20.
21. public static void main(String[] args) {
22.
23. for (Seasons s : Seasons.values()) {
24. System.out.println(s);
25. }
26. }
27.
28. }
运行上述代码你回得到 以下结果:
winter
spring
summer
fall
Enum的属性调用:
下面的代码展示了调用enum对象的方法,这也是它通常的用法:
Java代码
1. /**
2. * File: EnumSwitch.java
3. *
4. * Purpose: show how to switch against the values in an enum.
5. */
6. public class EnumSwitch {
7.
8. private enum Color {
9. red, blue, green
10. }
11.
12. // list the values
13.
14. public static void main(String[] args) {
15. // refer to the qualified value
16. doIt(Color.red);
17.
18. }
19.
20. /*
21. * note that you switch against the UNQUALIFIED name. that is,
22. * "case Color.red:" is a
23. *
24. * compiler error
25. */
26. private static void doIt(Color c) {
27. switch (c) {
28. case red:
29. System.out.println("value is " + Color.red);
30. break;
31. case green:
32. System.out.println("value is " + Color.green);
33. break;
34. case blue:
35. System.out.println("value is : " + Color.blue);
36. break;
37. default:
38. System.out.println("default");
39.
40. }
41. }
42. }
为enums添加属性和方法
enums也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样.
Java代码
1. /**
2. * 在枚举类中加入方法
3. * File: EnumWithMethods.java
4. *
5. * Purpose: show how to use an enum that also defines its own fields and methods
6. */
7. public class EnumWithMethods {
8.
9. // declare the enum and add values to it.
10. public enum Season {
11. winter, spring, summer, fall;
12.
13. private final static String location = "Phoenix";
14.
15. public static Season getBest() {
16. if (location.equals("Phoenix"))
17. return winter;
18. else
19. return summer;
20. }
21. }
22.
23. public static void main(String[] args) {
24. System.out.println(Season.getBest());
25.
26. }
27. }
就是这么的简单.但是有一点是需要注意的,那就是enums的值列表必须紧跟在enum声明,不然编译时将会出错.
Enums构造函数:
和类一样enums也可以有自己的构造函数,如下:
Java代码
1. /**
2. * 构造方式,创建枚举 title:
3. *
4. */
5. public class EnumConstructor {
6.
7. public static void main(String[] a) {
8. // call our enum using the values method
9. for (Temp t : Temp.values())
10. System.out.println(t + " is : " + t.getValue());
11. }
12.
13. // make the enum
14.
15. public enum Temp {
16.
17. absoluteZero(-459), freezing(32), boiling(212), paperBurns(451);
18.
19. // constructor here
20. Temp(int value) {
21. this.value = value;
22. }
23.
24. // regular field?but make it final,
25. // since that is the point, to make constants
26. private final int value;
27.
28. // regular get method
29. public int getValue() {
30. return value;
31. }
32. }
33.
34. }
输出结果是:
absoluteZero is : -459
freezing is : 32
boiling is : 212
paperBurns is : 451
尽管enums有这么多的属性,但并不是用的越多越好,如果那样还不如直接用类来的直接.enums的优势在定义int最终变量仅当这些值有一定特殊含义时.但是如果你需要的是一个类,就定义一个类,而不是enum.

3212

被折叠的 条评论
为什么被折叠?



