我有一个例子,我对输出感到困惑。

 enum Seasons {
    WINTER,
    SUMMER,
    SPRING,
    AUTUMN;
}
public class SeasonTest{
    public static void main(String[] args) {
    Seasons season1 = Seasons.WINTER;
    Seasons season2 = Seasons.SUMMER;
    Seasons season3 = Seasons.SPRING;
    Seasons season4 = Seasons.AUTUMN;
    System.out.println(season1.compareTo(season2));
    System.out.println(season3.compareTo(season4));
    System.out.println(season4.compareTo(season3));
    System.out.println(season2.compareTo(season1));
    System.out.println(season3.compareTo(season3));
    System.out.println(season1.compareTo(season4));
}
}


最后一行返回-3。这是为什么?它应返回-1,因为season1小于season4。为什么会返回-3?

谢谢。

最佳答案

允许compareTo()返回任何正数或负数或零。

它不需要专门返回+/- 1。

10-07 12:28