如何在构造函数中传递类型枚举的值?

时间:2018-02-17 17:27:37

标签: java enums

我对enum类型不太熟悉,我必须创建一个体育竞赛课程,该课程从课堂活动延伸出来。它需要2个新参数,其中一个是enum类型的季节。不知道如何实现它,以便我可以在初始化对象时选择其中一个。

package SportCompetition;
import Event.Event;

public class SportCompetition extends Event {
    enum Seasons {SUMMER,FALL,WINTER,SPRING};
    int numberOfActivities;

    public SportCompetition(int year, int month, 
                            int numberOfCities, int numberOfActivities) {
        super(year,month,numberOfCities);
        this.numberOfActivities = numberOfActivities;
    }
}

4 个答案:

答案 0 :(得分:2)

您应该在课堂外创建Enum。如果定义了Enum,则可以使用它,就像使用其他类声明变量并使用构造函数设置它一样。

尝试以下代码:

public class SportCompetition extends Event {

    int numberOfActivities;
    Season season;

    public SportCompetition(int year, int month, int numberOfCities, int numberOfActivities, Season season) {
        super(year, month, numberOfCities);
        this.numberOfActivities = numberOfActivities;
        this.season = season;
    }
}

enum Season {
    SUMMER, FALL, WINTER, SPRING
}

当您创建SportCompetition对象时,请创建如下所示的对象。

SportCompetition sportCompetition = new SportCompetition(2018,1,4,10,Seasons.WINTER);

答案 1 :(得分:0)

你只有在传递enum作为参数时遇到问题,所以我创建了一个构造函数,为简单起见只接受参数作为枚举,你可以将这个解决方案扩展到你的实际问题

创建Enum

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

现在你想从构造函数创建一个SportCompetition对象,它只接受一个枚举属性

SportCompetition sportCompetition = new SportCompetition(Seasons.SUMMER)

完整代码:

public class SportCompetition extends Event {
  int numberOfActivities;
  Seasons season;

  public SportCompetition(Seasons season) {
    this.season = season;
  }
  public static void main(String[] args) {
      SportCompetition sportCompetition = new SportCompetition(Seasons.SPRING);
  }
}
enum Seasons {SUMMER,FALL,WINTER,SPRING};

传递枚举new SportCompetition(Seasons.SUMMER)

答案 2 :(得分:0)

enum Season {
    SPRING, SUMMER, FALL, WINTER;
    static Season[] Seasons = {Season.SPRING, Season.SUMMER, Season.FALL, Season.WINTER};
    static public Season fromMonth (int m) {
        int idx = ((m + 10) % 12)/3;
        return Seasons[idx];
    }
}

for (int i = 0; i < 24; ++i)
    System.out.printf ("%2d %2d %s\n", i, ((i + 10) % 12), Season.fromMonth (i)); 

好的,首先,您必须小心 - 您的月份字面值(1 == Jan)或索引(0 = Jan)?其次,季节不会在月份边界上发生变化,还是在金融季节发生变化?所以这只是概念的证明。但是如果你想要使用这种简化的日历概念,那么使用线性函数将月数映射到季节是可行的方法。

并命名 - 唯一的事情是Season.WINTER而不是Informatiker Alfred Winter而不是Seasons.Winter。季节将是,并且是季节的集合。

-> for (int i = 0; i < 24; ++i) System.out.printf ("%2d %2d %s\n", i, ((i + 10) % 12),  Season.fromMonth (i)); 
 0 10 WINTER
 1 11 WINTER
 2  0 SPRING
 3  1 SPRING
 4  2 SPRING
 5  3 SUMMER
 6  4 SUMMER
 7  5 SUMMER
 8  6 FALL
 9  7 FALL
10  8 FALL
11  9 WINTER
12 10 WINTER
13 11 WINTER
14  0 SPRING
15  1 SPRING
16  2 SPRING
17  3 SUMMER
18  4 SUMMER
19  5 SUMMER
20  6 FALL
21  7 FALL
22  8 FALL
23  9 WINTER

我想没有人理解,为什么在枚举中,我们必须在Season.SPRING中重复名称Season。也许我错过了什么。

答案 3 :(得分:0)

我知道你已经得到了答案,但可能有理由将校友保持在课堂内以实现分离。

您可以使用static enum并享受将enum放入其相关课程本身的架构优势。

package SportCompetition;
import Event.Event;

public class SportCompetition extends Event {
    static enum Season {
     SUMMER,FALL,WINTER,SPRING
    }
    int numberOfActivities;
    Season season;
    public SportCompetition(int year, int month, int numberOfCities, int numberOfActivities, Season season) {
    super(year, month, numberOfCities);
    this.numberOfActivities = numberOfActivities;
    this.season = season;
}
}

并创建如下对象:

SportCompetition sportCompetition = new SportCompetition(2018,1,4,10,SportCompetition.Season.WINTER);