有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有逻辑OR选择的Java枚举

我想实现一个方法,返回一个或多个定义为enum的允许操作列表

例如,我的方法应该是这样的:

public enum getAllowedActions() {
  return // (( 'something like' Actions.ACTION1 & Actions.ACTION2 ));
}

然后在另一个位置读取结果,如:

if (getAllowedActions() == Actions.ACTION1) {
  // do something...
}

与:

public class enum {
  ACTION1, ACTION2;
}

谢谢。 斯特凡诺


共 (1) 个答案

  1. # 1 楼答案

    听起来你只是在找^{}

    public EnumSet<Action> getAllowedActions() {
        return EnumSet.of(Action.ACTION1, Action.ACTION2);
    }
    
    ...
    if (getAllowedAction().contains(Action.ACTION1)) {
        ...
    }