具有逻辑或选择的 Java ENUM

发布于 2024-12-17 23:35:37 字数 413 浏览 3 评论 0原文

我想实现一种方法,返回一个或一组定义为枚举的允许操作。

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

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

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

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

with:

public class enum {
  ACTION1, ACTION2;
}

谢谢。 斯特凡诺.

I would like to implement a method that returns one or a list of allowed Action defined as enum.

For example my method should be like this:

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

and then read the results in another position like:

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

with:

public class enum {
  ACTION1, ACTION2;
}

Thanks.
Stefano.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-12-24 23:35:37

听起来您只是在寻找 EnumSet

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

...
if (getAllowedAction().contains(Action.ACTION1)) {
    ...
}

Sounds like you're just lookng for EnumSet:

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

...
if (getAllowedAction().contains(Action.ACTION1)) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文