简体   繁体   English

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

[英]How can I pass a value of type enum in a constructor?

I am not too familiar with enum type and I must create a Sport competition class which extends from the class event. 我对枚举类型不太熟悉,我必须创建一个体育竞赛类,该类是从类事件中扩展而来的。 It takes in 2 new parameters, one of them being the season of type enum. 它接受两个新参数,其中之一是枚举类型的季节。 Not sure how to implement this so that I can choose one of them when initializing the object. 不知道如何实现这一点,以便在初始化对象时可以选择其中之一。

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;
    }
}

You should create your Enum outside the class. 您应该在课程外创建Enum When you have an Enum defined you can use it like you use other classes to declare a variable and set it using a constructor. 定义了Enum ,就可以像使用其他类来声明变量并使用构造函数进行设置一样使用它。

Try the Below code: 试试下面的代码:

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
}

When you create the object of SportCompetition create the object like below. 当您创建SportCompetition的对象时, SportCompetition创建如下所示的对象。

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

You only have problem in passing enum as argument, so I am creating a constructor which accepts only argument as enum for simplicity and you can extend this solution to your actual problem 您只在将枚举作为参数传递时遇到问题,所以我正在创建一个构造器,为了简单起见,它仅接受参数作为枚举,您可以将此解决方案扩展到实际问题

Creating a Enum 创建一个Enum

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

Now you want to create an object of SportCompetition from the constructor which accepts only one Enum attribute 现在,您要从仅接受一个Enum属性的构造函数创建一个SportCompetition对象。

SportCompetition sportCompetition = new SportCompetition(Seasons.SUMMER)

Complete code: 完整的代码:

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};

passing Enum new SportCompetition(Seasons.SUMMER) 通过Enum new SportCompetition(Seasons.SUMMER)

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)); 

Okay, first, you have to take care - are your months literal values (1==Jan) or indexes (0=Jan)? 好的,首先,您必须注意-您的月份是字面值(1 == Jan)还是索引(0 = Jan)? Second, Seasons don't change on month borders, or do they in finance seasons? 其次,季节在月份边界上不会改变,还是在财务季节内改变? So it's just a prove of concept. 因此,这只是概念的证明。 But if you want to go with this simplified calender concept, using a linear function to map months to Seasons is the way to go. 但是,如果您想使用这种简化的压光机概念,则可以使用线性函数将月份映射到Seasons。

And naming - the single thing is a Season.WINTER not Informatiker Alfred Winter and not Seasons.Winter. 命名-唯一的问题是Season.WINTER,而不是Informatiker Alfred Winter,而不是Seasons.Winter。 Seasons would be, and is, a Collection of Season. 季节将是并且是季节的集合。

-> 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

I guess nobody understands, why in the enum, we have to repeat the name Season in Season.SPRING and so on. 我猜没有人理解,为什么在枚举中我们必须重复命名Season in Season.SPRING等。 Maybe I'm missing something. 也许我想念一些东西。

I know you have got the answer but there possibly could be the reason to keep the enum inside the class to achieve separation of concern. 我知道您已经找到了答案,但是可能有理由将枚举保留在类中以实现关注点分离。

You could use static enum and enjoy the architectural benefit of putting enum inside its relevant class itself. 您可以使用static 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;
}
}

And create object like below: 并创建如下对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM