如何调用getSeason(Months.OCTOBER)?

How to call getSeason(Months.OCTOBER)?

我的问题是关于 enums 和 switch 表达式.

我在尝试使用 getSeason(Months.OCTOBER) 调用方法时遇到困难。

如何在main方法中调用这个方法来打印“SPRING”?我认为问题不在 Season 方法中,而是在 main() 方法中。

代码:

public class Seasons {
  private static Seasons season;

  enum Seasons {SPRING, SUMMER, WINTER, AUTUMN};

  enum Months { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, 
    SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};

  public static Seasons getSeason(Months month) {
    Seasons season = null;
    switch (month) {
      case SEPTEMBER:
      case OCTOBER:
      case NOVEMBER:
        season = Seasons.SPRING;
        break;
      case DECEMBER:
      case JANUARY:
      case FEBRUARY:
        season = Seasons.WINTER;
        break;
      case JUNE:
      case JULY:
      case AUGUST:
        season = Seasons.SUMMER;
        break;
      case MARCH:
      case APRIL:
      case MAY:
        season = Seasons.AUTUMN;
        break;
    }
    return season;
  }

  public static void main(String[] args) {

    getSeason(Months.OCTOBER); //How can I print “SPRING” ?

  }
}

方法 getSeason() 正确地完成了它的工作,您在控制台上看不到任何内容的原因是方法调用的结果被忽略了。

只需这样做:

System.out.println(getSeason(Months.OCTOBER));

顺便说一下,如果您使用 Java 14 +,您的 getSeason() 方法可以以更简洁的方式实现。

    public static Seasons getSeason(Months month) {
        return switch (month) {
            case SEPTEMBER, OCTOBER, NOVEMBER -> Seasons.SPRING;
            case DECEMBER, JANUARY, FEBRUARY -> Seasons.WINTER;
            case JUNE, JULY, AUGUST -> Seasons.SUMMER;
            case MARCH, APRIL, MAY -> Seasons.AUTUMN;
        };
    }

这个...

public class Seasons {
  enum Seasons {SPRING, SUMMER, WINTER, AUTUMN};

给我没完没了的问题,编译器似乎不喜欢你重新声明 Seasons

您似乎还错过了封装某些功能的机会,例如,如果您可以直接询问 MONTH 它代表什么季节,那不是更容易吗?

所以,我首先给你重命名 enums 使它们是复数,然后添加对 Month enum 的支持以获得当前的 Season

public class Seasons {

    enum Season {
        SPRING, SUMMER, WINTER, AUTUMN
    };

    enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
        SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;

        public Season getSeason() {
            switch (this) {
                case SEPTEMBER:
                case OCTOBER:
                case NOVEMBER:
                    return Season.SPRING;
                case DECEMBER:
                case JANUARY:
                case FEBRUARY:
                    return Season.WINTER;
                case JUNE:
                case JULY:
                case AUGUST:
                    return Season.SUMMER;
                case MARCH:
                case APRIL:
                case MAY:
                    return Season.AUTUMN;
            }
            return null;
        }
    };

    public static void main(String[] args) {
        System.out.println(Month.OCTOBER.getSeason());
    }
}

是正确的。我将添加一些关于命名的内容和一些提示。

我们已经有了 java.time.Month 枚举。无需重新发明。

一般情况下,枚举应该以单数形式命名。例如,java.time.Monthjava.time.DayOfWeek。所以,Season 而不是 Seasons.

java.timeclasses(JSR 310) established some naming conventions that have proven to be sensible and handy. For moving from the more specific to the more general, meaning we are losing information, use from. For example, java.time.YearMonth offers a static factory method from 从日期对象生成年月值等。所以Season#from( java.time.Month ).

并考虑添加一种方法来生成显示名称,而不是枚举对象的全大写名称。对于小型特定项目,针对特定语言对这种方法的结果进行硬编码。如果重新使用此 class,请考虑通过传递的 Locale 参数添加本地化,​​如 java.time classes 中所示。例如,java.time.Month#getDisplayName( TextStyle , Locale ).

enum Season {
        SPRING ( "Spring" ), 
        SUMMER ( "Summer" ), 
        WINTER ( "Winter" ), 
        AUTUMN ( "Autumn" );
    
    private String displayName ;
    
    Season ( String displayName ) {
        this.displayName = displayName ;
    }
    
    public String getDisplayName() { return this.displayName ; }
    
    public Season from( java.time.Month month ) {
        Objects.requireNonNull( month ) ;
        return switch ( month ) {
            case SEPTEMBER, OCTOBER, NOVEMBER -> Season.SPRING;
            case DECEMBER, JANUARY, FEBRUARY -> Season.WINTER;
            case JUNE, JULY, AUGUST -> Season.SUMMER;
            case MARCH, APRIL, MAY -> Season.AUTUMN;
        };
    }
}

用法:

System.out.println( Season.from( Month.JANUARY ) ) ;
System.out.println( Season.from( Month.JANUARY ).getDisplayName() ) ;

WINTER

Winter

顺便说一句,提示:在 Java 16 及更高版本中,现在可以在本地定义枚举。这来自 records.

的工作