我是Java新手。我需要检查enum元素是否进入enum集中。

在德尔福:

type
  TWeekEnum = (weMonday, weTuesday, weWednesday, weThursday, weFriday, weSaturday, weSunday)
  TWeekSetEnum = (weSaturday, weSunday)


  if (weSunday in (TWeekSetEnum))
  ...

Java的?

最佳答案

您可以通过这种方式定义enum,然后在其上将子集创建为静态方法。

public enum TWeekEnum {
    weMonday, weTuesday, weWednesday, weThursday, weFriday, weSaturday, weSunday;

    public static EnumSet<TWeekEnum> getWeekend() {
        return EnumSet.of(weSaturday, weSunday);
    }

    public static EnumSet<TWeekEnum> getWeekDays() {
        return EnumSet.complementOf(getWeekend());
    }
}

然后,您可以检查它是否包含您选择的项目,像这样
TWeekEnum.getWeekend().contains(TWeekEnum.weTuesday)

07-24 22:30