枚举类的使用

1.枚举类的理解:类的对象只有有限个,确定的。我们称此类为枚举类
2.当需要定义一组常量时,强烈建议使用枚举类
3.如果枚举类中只有一个对象,则可以作为单例模式的实现方式

定义枚举类

方式一

jdk5.0之前,自定义枚举类

  1. public class SeasonTest {
  2. public static void main(String[] args) {
  3. Season spring = Season.SPRING;
  4. System.out.println(spring);
  5. }
  6. }
  7. //自定义枚举类
  8. class Season{
  9. //1.声明Season对象的属性:private final修饰
  10. private final String seasonName;
  11. private final String seasonDesc;
  12. //2.私有化类的构造器,并给对象属性赋值
  13. private Season (String seasonName,String seasonDesc){
  14. this.seasonName = seasonName;
  15. this.seasonDesc = seasonDesc;
  16. }
  17. //3.提供当前枚举类的多个对象:public static final的
  18. public static final Season SPRING = new Season("春天","春暖花开" );
  19. public static final Season SUMMER = new Season("夏天","夏日炎炎" );
  20. public static final Season AUTUMN = new Season("秋天","秋高气爽" );
  21. public static final Season WINTER = new Season("冬天","冰树银花" );
  22. //4.其他诉求:获取枚举类对象的属性
  23. public String getSeasonName() {
  24. return seasonName;
  25. }
  26. public String getSeasonDesc() {
  27. return seasonDesc;
  28. }
  29. //5.提供toString方法
  30. @Override
  31. public String toString() {
  32. return "Season{" +
  33. "seasonName='" + seasonName + '\'' +
  34. ", seasonDesc='" + seasonDesc + '\'' +
  35. '}';
  36. }
  37. }

方式二

jdk5.0,可以使用enum关键字定义枚举类
说明:定义的枚举类默认继承于java.lang.Enum类

  1. public class SeasonTest1 {
  2. public static void main(String[] args) {
  3. Season1 summer = Season1.SUMMER;
  4. System.out.println(summer);
  5. }
  6. }
  7. //自定义枚举类
  8. enum Season1{
  9. //1.提供当前枚举类的对象,多个对象之间用”,“隔开,最后一个对象”;“结束
  10. SPRING ("春天","春暖花开" ),
  11. SUMMER ("夏天","夏日炎炎" ),
  12. AUTUMN ("秋天","秋高气爽" ),
  13. WINTER ("冬天","冰树银花" );
  14. //2.声明Season对象的属性:private final修饰
  15. private final String seasonName;
  16. private final String seasonDesc;
  17. //3.私有化类的构造器,并给对象属性赋值
  18. private Season1 (String seasonName,String seasonDesc){
  19. this.seasonName = seasonName;
  20. this.seasonDesc = seasonDesc;
  21. }
  22. //4.其他诉求:获取枚举类对象的属性
  23. public String getSeasonName() {
  24. return seasonName;
  25. }
  26. public String getSeasonDesc() {
  27. return seasonDesc;
  28. }
  29. //5.提供toString方法
  30. //@Override
  31. //public String toString() {
  32. //return "Season{" +
  33. //"seasonName='" + seasonName + '\'' +
  34. //", seasonDesc='" + seasonDesc + '\'' +
  35. //'}';
  36. //}
  37. }

Enum类中常用方法

  1. //toString():
  2. System.out.println(summer.toString());
  3. //value():
  4. Season1[] values = Season1.values();
  5. for (int i =0;i < values.length;i++){
  6. System.out.println(values[i]);
  7. }
  8. //valueOf(String objName):返回枚举类中对象名是objName的对象
  9. //如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
  10. Season1 winter = Season1.valueOf("WINTER");
  11. System.out.println(winter);

enum关键字定义枚举类实现接口情况

①实现接口,在enum类中实现抽象方法

  1. interface Info{
  2. void show();
  3. }
  4. public void show(){
  5. System.out.println("这是一个季节");
  6. }

②让枚举类的对象分别实现接口中的抽象方法

  1. interface Info{
  2. void show();
  3. }
  4. SPRING ("春天","春暖花开" ){
  5. @Override
  6. public void show() {
  7. }
  8. },
  9. SUMMER ("夏天","夏日炎炎" ){
  10. @Override
  11. public void show() {
  12. }
  13. },
  14. AUTUMN ("秋天","秋高气爽" ){
  15. @Override
  16. public void show() {
  17. }
  18. },
  19. WINTER ("冬天","冰树银花" ){
  20. @Override
  21. public void show() {
  22. }
  23. };