Java枚举类的5种用法详解

Java枚举类的5种用法详解-mikechen

Java 枚举是一个特殊的类,一般表示一组常量,比如一年的 4 个季节,一个年的 12 个月份,一个星期的 7 天,方向有东南西北等。

Java枚举写法

Java 枚举类使用 enum 关键字来定义,各个常量使用逗号 , 来分割。

例如定义一个颜色的枚举类。

  1. enum Color
  2. {
  3. RED, GREEN, BLUE;
  4. }

以上枚举类 Color 颜色常量有 RED, GREEN, BLUE,分别表示红色,绿色,蓝色。

内部类中使用枚举

枚举类也可以声明在内部类中

  1. public class Test
  2. {
  3. enum Color
  4. {
  5. RED, GREEN, BLUE;
  6. }
  7. // 执行输出结果
  8. public static void main(String[] args)
  9. {
  10. Color c1 = Color.RED;
  11. System.out.println(c1);
  12. }
  13. }

执行以上代码输出结果为:

RED

每个枚举都是通过 Class 在内部实现的,且所有的枚举值都是 public static final 的。

以上的枚举类 Color 转化在内部类实现:

  1. class Color
  2. {
  3. public static final Color RED = new Color();
  4. public static final Color BLUE = new Color();
  5. public static final Color GREEN = new Color();
  6. }

迭代枚举元素

可以使用 for 语句来迭代枚举元素

  1. enum Color
  2. {
  3. RED, GREEN, BLUE;
  4. }
  5. public class MyClass {
  6. public static void main(String[] args) {
  7. for (Color myVar : Color.values()) {
  8. System.out.println(myVar);
  9. }
  10. }
  11. }

执行以上代码输出结果为:

RED

GREEN

BLUE

在 switch 中使用枚举类

枚举类常应用于 switch 语句中:

  1. enum Color
  2. {
  3. RED, GREEN, BLUE;
  4. }
  5. public class MyClass {
  6. public static void main(String[] args) {
  7. Color myVar = Color.BLUE;
  8. switch(myVar) {
  9. case RED:
  10. System.out.println("红色");
  11. break;
  12. case GREEN:
  13. System.out.println("绿色");
  14. break;
  15. case BLUE:
  16. System.out.println("蓝色");
  17. break;
  18. }
  19. }
  20. }

执行以上代码输出结果为:

蓝色

values(), ordinal() 和 valueOf() 方法

enum 定义的枚举类默认继承了 java.lang.Enum 类,并实现了 java.lang.Seriablizable 和 java.lang.Comparable 两个接口。

values(), ordinal() 和 valueOf() 方法位于 java.lang.Enum 类中:

values() 返回枚举类中所有的值。

ordinal()方法可以找到每个枚举常量的索引,就像数组索引一样。

valueOf()方法返回指定字符串值的枚举常量。

  1. enum Color
  2. {
  3. RED, GREEN, BLUE;
  4. }
  5. public class Test
  6. {
  7. public static void main(String[] args)
  8. {
  9. // 调用 values()
  10. Color[] arr = Color.values();
  11. // 迭代枚举
  12. for (Color col : arr)
  13. {
  14. // 查看索引
  15. System.out.println(col + " at index " + col.ordinal());
  16. }
  17. // 使用 valueOf() 返回枚举常量,不存在的会报错 IllegalArgumentException
  18. System.out.println(Color.valueOf("RED"));
  19. // System.out.println(Color.valueOf("WHITE"));
  20. }
  21. }

执行以上代码输出结果为:

RED at index 0

GREEN at index 1

BLUE at index 2

RED

枚举类成员

枚举跟普通类一样可以用自己的变量、方法和构造函数,构造函数只能使用 private 访问修饰符,所以外部无法调用。

枚举既可以包含具体方法,也可以包含抽象方法。 如果枚举类具有抽象方法,则枚举类的每个实例都必须实现它。

  1. enum Color
  2. {
  3. RED, GREEN, BLUE;
  4. // 构造函数
  5. private Color()
  6. {
  7. System.out.println("Constructor called for : " + this.toString());
  8. }
  9. public void colorInfo()
  10. {
  11. System.out.println("Universal Color");
  12. }
  13. }
  14. public class Test
  15. {
  16. // 输出
  17. public static void main(String[] args)
  18. {
  19. Color c1 = Color.RED;
  20. System.out.println(c1);
  21. c1.colorInfo();
  22. }
  23. }

执行以上代码输出结果为:

Constructor called for : RED

Constructor called for : GREEN

Constructor called for : BLUE

RED

Universal Color。

评论交流
    说说你的看法
欢迎您,新朋友,感谢参与互动!