首页 > 代码库 > 枚举类的介绍

枚举类的介绍

  • 枚举类,即对象为可穷举的类
  • JDK 1.5 新增的 enum 关键字用于定义枚举类,在这之前,需要自定义枚举类
  • 若枚举只有一个成员(对象),则可以作为一种单例模式的实现方式——单例(单子)设计模式(Singleton)

自定义枚举类:
  1. public class TestSeason {
  2. public static void main(String[] args) {
  3. Season spring = Season.SPRING;
  4. Season summer = Season.SUMMER;
  5. Season autumn = Season.AUTUMN;
  6. Season winter = Season.WINTER;
  7. System.out.println(spring);// Season [seasonName=春天]
  8. System.out.println(summer);// Season [seasonName=夏天]
  9. System.out.println(autumn);// Season [seasonName=秋天]
  10. System.out.println(winter);// Season [seasonName=冬天]
  11. }
  12. }
  13. class Season {
  14. // 1.提供以 private final 修饰的类属性
  15. private final String seasonName;
  16. // 2.在以 private 修饰的构造器中初始化类属性
  17. private Season(String seasonName) {
  18. this.seasonName = seasonName;
  19. }
  20. // 3.提供公共的方法来调用属性
  21. public String getSeasonName() {
  22. return this.seasonName;
  23. }
  24. // 4.创建以 public static final 修饰的该枚举类的对象
  25. public static final Season SPRING = new Season("春天");
  26. public static final Season SUMMER = new Season("夏天");
  27. public static final Season AUTUMN = new Season("秋天");
  28. public static final Season WINTER = new Season("冬天");
  29. @Override
  30. public String toString() {
  31. return "Season [seasonName=" + seasonName + "]";
  32. }
  33. }

使用 enum 关键字定义枚举类及其特性:
  1. public class TestSeason {
  2. public static void main(String[] args) {
  3. Season spring = Season.SPRING;
  4. Season summer = Season.SUMMER;
  5. Season autumn = Season.AUTUMN;
  6. Season winter = Season.WINTER;
  7. System.out.println(spring);// Season [seasonName=春天]
  8. System.out.println(summer);// Season [seasonName=夏天]
  9. System.out.println(autumn);// Season [seasonName=秋天]
  10. System.out.println(winter);// Season [seasonName=冬天]
  11. // 枚举类常用的方法
  12. // 1.以数组的形式返回所有枚举类的对象:values();
  13. Season[] seasons = Season.values();
  14. for (Season s : seasons) {
  15. System.out.println(s);
  16. } // Season [seasonName=春天]
  17. // Season [seasonName=夏天]
  18. // Season [seasonName=秋天]
  19. // Season [seasonName=冬天]
  20. // 2.返回以指定字符串为对象名的对象(要求形参 str 是枚举对象的名字,
  21. // 否则报IllegalArgumentException):valueOf(String str);
  22. String str = "SPRING";
  23. System.out.println(Season.valueOf(str).getSeasonName());// 春天
  24. // 接口的特殊实现
  25. Season.valueOf(str).show();// 春暖花开
  26. }
  27. }
  28. interface Info {
  29. void show();
  30. }
  31. enum Season implements Info {
  32. // 与自定义枚举类对比:对象声明要放最前面,重复的部分删除,对象之间以逗号分隔
  33. // 枚举类实现接口,可以让不同对象以不同的方式实现接口中的抽象方法(也可以让类
  34.    // 实现,各对象共享)
  35. SPRING("春天") {
  36. public void show() {
  37. System.out.println("春暖花开");
  38. }
  39. },
  40. SUMMER("夏天") {
  41. public void show() {
  42. System.out.println("夏日炎炎");
  43. }
  44. },
  45. AUTUMN("秋天") {
  46. public void show() {
  47. System.out.println("秋高气爽");
  48. }
  49. },
  50. WINTER("冬天") {
  51. public void show() {
  52. System.out.println("白雪皑皑");
  53. }
  54. };
  55. private final String seasonName;
  56. private Season(String seasonName) {
  57. this.seasonName = seasonName;
  58. }
  59. public String getSeasonName() {
  60. return this.seasonName;
  61. }
  62. @Override
  63. public String toString() {
  64. return "Season [seasonName=" + seasonName + "]";
  65. }
  66. }

枚举类的介绍