分享

Java枚举的使用

 沙门空海 2020-03-15

枚举类型可以取代以往常量的定义方式,即将常量封装在类或接口中。此外,枚举类型还提供了安全检查功能。枚举类型本质上还是以类的形式存在。

1、使用枚举类型设置常量

以往设置常量,通常将常量放置在接口中,这样在程序中就可以直接使用了,并且该常量不能被修改,因为在接口中定义的常量时,该常量的修饰符为final与static。

  1. public interface Constants
  2. {
  3. public static final int RED = 1;
  4. public static final int BLUE = 2;
  5. public static final int GREEN = 3;
  6. }

使用枚举定义常量的语法如下:

  1. public enum ColorEnum
  2. {
  3. RED,
  4. BLUE,
  5. GREEN
  6. }

命名规范:

final常量:使用大写字母命名,并且中间使用下划线进行连接。

enum枚举:使用大写字母命名,并且中间使用下划线进行连接。

示例:枚举类型的使用。

  1. public static void doit(ColorEnum c)
  2. {
  3. switch (c)
  4. {
  5. case RED:
  6. System.out.println("This is RED");
  7. break;
  8. case BLUE:
  9. System.out.println("This is BLUE");
  10. break;
  11. case GREEN:
  12. System.out.println("This is GREEN");
  13. break;
  14. default:
  15. break;
  16. }
  17. }
  18. public static void main(String[] args)
  19. {
  20. doit(ColorEnum.RED);
  21. }

 

2、深入了解枚举类型

枚举类型较传统定义常量的方式,除了具有参数类型检测的优势之外,还具有其他方面的优势。

2.1 操作枚举类型成员的方法

用户可以将一个枚举类型看作是一个类,它继承于java.lang.Enum类,当定义一个枚举类型时,每一个枚举类型成员都可以看作是枚举类型的一个实例,这些枚举类型成员默认都被final、public、static所修饰,所以当使用枚举类型成员时直接使用枚举类型名称调用枚举类型成员即可。

由于枚举类型对象继承与java.lang.Enum类,所以该类中一些操作枚举类型的方法都可以应用到枚举型中。

枚举类型的常用方法:

方法名称 具体含义 使用方法
values() 该方法可以将枚举类型成员以数组的形式返回 枚举类型名称.values()
valueOf() 该方法可以实现将普通字符串转换为枚举实例 枚举类型名称.valueOf("ABC")
compareTo() 该方法用于比较两个枚举对象在定义时的顺序 枚举对象.compareTo()
ordinal() 该方法用于得到枚举成员的位置索引 枚举对象.ordinal()

(1)values()方法

该方法可以将枚举类型成员以数组的形式返回,也可以通过该方法获取枚举类型的成员。

示例:使用枚举类型中的values()方法获取枚举类型中的成员变量。

  1. /**
  2. * 使用枚举类型中的values()方法获取枚举类型中的成员变量
  3. *
  4. * @author pan_junbiao
  5. *
  6. */
  7. public class ShowEnum
  8. {
  9. enum ColorEnum
  10. {
  11. RED, BLUE, GREEN
  12. }
  13. // 循环由values()方法返回的数组
  14. public static void main(String[] args)
  15. {
  16. System.out.println("方式一:");
  17. for (int i = 0; i < ColorEnum.values().length; i++)
  18. {
  19. // 将枚举成员变量打印
  20. System.out.println("枚举类型成员变量:" + ColorEnum.values()[i]);
  21. }
  22. System.out.println("方式二:");
  23. for (ColorEnum c : ColorEnum.values())
  24. {
  25. // 将枚举成员变量打印
  26. System.out.println("枚举类型成员变量:" + c);
  27. }
  28. }
  29. }

执行结果:

(2)valueOf()与compareTo()方法

枚举类型中静态方法valueOf()可以实现将普通字符串转换为枚举实例,而compareTo()方法用于比较两个枚举对象在定义时的顺序。

示例:枚举中valueOf()与compareTo()方法的使用。

  1. /**
  2. * 枚举中valueOf()与compareTo()方法的使用
  3. *
  4. * @author pan_junbiao
  5. *
  6. */
  7. public class EnumMethodTest
  8. {
  9. enum ColorEnum
  10. {
  11. RED, BLUE, GREEN
  12. }
  13. // 定义比较枚举类型方法,参数类型为枚举类型
  14. public static void compare(ColorEnum c)
  15. {
  16. // 根据values()方法返回的数组做循环操作
  17. for (int i = 0; i < ColorEnum.values().length; i++)
  18. {
  19. // 将比较结果返回
  20. System.out.println(c + "与" + ColorEnum.values()[i] + "的比较结果为:" + c.compareTo(ColorEnum.values()[i]));
  21. }
  22. }
  23. public static void main(String[] args)
  24. {
  25. // 使用valueOf()将字符串转换为枚举实例
  26. ColorEnum c = ColorEnum.valueOf("BLUE");
  27. compare(c);
  28. }
  29. }

执行结果:

说明:调用compareTo()方法返回的结果,正值代表方法中的参数在调用该方法的枚举对象位置之前;0代表两个相互比较的枚举成员的位置相同;负值代表方法中参数在调用该方法的枚举对象位置之后。

(3)ordinal()方法

该方法用于得到枚举成员的位置索引。

示例:枚举中ordinal()方法的使用。

  1. /**
  2. * 枚举中ordinal()方法的使用
  3. *
  4. * @author pan_junbiao
  5. *
  6. */
  7. public class EnumOrdinalTest
  8. {
  9. public enum ColorEnum
  10. {
  11. RED, BLUE, GREEN
  12. }
  13. public static void main(String[] args)
  14. {
  15. for (int i = 0; i < ColorEnum.values().length; i++)
  16. {
  17. // 在循环中获取枚举类型成员的索引位置
  18. System.out.println(ColorEnum.values()[i] + "在枚举类型中位置索引值" + ColorEnum.values()[i].ordinal());
  19. }
  20. }
  21. }

执行结果:

2.2 枚举类型中的构造方法

在枚举类型中,可以添加构造方法,但是规定这个构造方法必须为private修饰符修饰。

示例:在枚举类型中,可以添加构造方法。

  1. /**
  2. * 在枚举类型中添加构造方法
  3. *
  4. * @author pan_junbiao
  5. *
  6. */
  7. public class EnumIndexTest
  8. {
  9. enum ColorEnum
  10. {
  11. RED(1, "我是红色"), BLUE(2, "我是蓝色"), GREEN(3, "我是绿色");
  12. private final int value;
  13. private final String description;
  14. private ColorEnum(int value, String description)
  15. {
  16. this.value = value;
  17. this.description = description;
  18. }
  19. public int getValue()
  20. {
  21. return this.value;
  22. }
  23. public String getDescription()
  24. {
  25. return this.description;
  26. }
  27. public static ColorEnum valueOf(int value)
  28. {
  29. switch (value)
  30. {
  31. case 1:
  32. return ColorEnum.RED;
  33. case 2:
  34. return ColorEnum.BLUE;
  35. case 3:
  36. return ColorEnum.GREEN;
  37. default:
  38. return null;
  39. }
  40. }
  41. }
  42. public static void main(String[] args)
  43. {
  44. for (ColorEnum c : ColorEnum.values())
  45. {
  46. System.out.println("枚举成员:" + c + " 值:" + c.getValue() + " 描述:" + c.getDescription());
  47. }
  48. System.out.println("值转换成枚举:" + ColorEnum.valueOf(2));
  49. System.out.println("字符串转换成枚举:" + ColorEnum.valueOf("GREEN"));
  50. }
  51. }

执行结果:

2.3 枚举中实现接口

除了可以使用上述示例的方法定义getDescription()方法获取枚举类型成员变量是的描述之外,还可以将这个getDescription()方法放置在接口中,使枚举类型实现接口,然后使每个枚举类型实现接口中的方法。

示例:在项目中创建d接口和枚举类型的AnyEnum类,在枚举类型AnyEnum类中实现带方法的接口,使每个枚举类型成员实现该接口中的方法。

  1. interface d
  2. {
  3. public int getValue();
  4. public String getDescription();
  5. }
  6. /**
  7. * 枚举中实现接口
  8. *
  9. * @author pan_junbiao
  10. *
  11. */
  12. public enum AnyEnum implements d
  13. {
  14. RED
  15. {
  16. public int getValue()
  17. {
  18. return 1;
  19. }
  20. public String getDescription()
  21. {
  22. return "我是红色";
  23. }
  24. },
  25. BLUE
  26. {
  27. public int getValue()
  28. {
  29. return 2;
  30. }
  31. public String getDescription()
  32. {
  33. return "我是蓝色";
  34. }
  35. },
  36. GREEN
  37. {
  38. public int getValue()
  39. {
  40. return 3;
  41. }
  42. public String getDescription()
  43. {
  44. return "我是绿色";
  45. }
  46. };
  47. public static void main(String[] args)
  48. {
  49. for (AnyEnum c : AnyEnum.values())
  50. {
  51. System.out.println("枚举成员:" + c + " 值:" + c.getValue() + " 描述:" + c.getDescription());
  52. }
  53. }
  54. }

执行结果:

 

3、使用枚举类型的优势

枚举类型声明提供了一种用户友好的变量定义方法,枚举了某种数据类型所有可能出现的值。总结枚举类型,它具有以下特点:

(1)类型安全。

(2)紧凑有效的数据定义。

(3)可以和程序其他部分完美交互。

(4)运行效率高。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多