java 如何要求泛型参数是实现接口的枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1070703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:02:27  来源:igfitidea点击:

How can I require a generic parameter to be an enum that implements an interface?

javagenericsenums

提问by Sbodd

I'm not 100% convinced that this is a good idea, but I bumped into some code today that's currently implemented as:

我不是 100% 相信这是一个好主意,但我今天遇到了一些目前实现的代码:

class MyWidget <T extends Enum<T> > {
  MyWidget(Map<T, Integer> valueMap) {
    mValueMap = valueMap;
  }

  Map<T, Integer> mValueMap;
}

where MyWidgetthen offers methods that use mValueMapto convert the passed-in Enumto/from an Integer.

where MyWidgetthen 提供mValueMap用于将传入的转换Enum为/从Integer.

What I was considering doing was trying to refactor this, so that I'd declare my enumeration:

我正在考虑做的是尝试重构它,以便我声明我的枚举:

interface MyInterface {
  public Integer getValue();
}

enum MyEnum implements MyInterface {
  foo, bar;
  public Integer getValue() {
    return ordinal();
  }
}

And I'd then be able to rewrite MyWidgetinto something that looked vaguely like this:

然后我就可以改写MyWidget成看起来像这样的东西:

public class MyWidget<T extends Enum<T> extends MyInterface> {
  ...
}

and would then be able to call the getValue()method from MyInterfaceon T-type objects within MyWidget. The problem, of course, is that "<T extends Enum<T> extends MyInterface>" isn't valid syntax. Is there any way to pull this off?

然后将能够调用getValue()从方法MyInterfaceT型内不提出异议MyWidget。当然,问题在于“ <T extends Enum<T> extends MyInterface>” 不是有效的语法。有什么办法可以解决这个问题吗?

I don't want to just have MyWidget<T extends MyInterface>, because it's also important that T be an enumeration.

我不想只拥有MyWidget<T extends MyInterface>,因为 T 是枚举也很重要。

Thanks in advance!

提前致谢!

回答by Michael Myers

Use an '&' instead:

改用“ &”:

public class MyWidget<T extends Enum<T> & MyInterface> {
    ...
}

The JLScalls this an "intersection type", but I can find no mention of it in the Java tutorials. I'll just say that it does exactly what you were wishing that "extends" would do.

JLS称这是“交集型”,但我能找到在Java教程没有提到它。我只想说它完全符合您希望“ extends”会做的事情。

Also, I should mention that you can have as many types as you want in the intersection type. So if you wanted, you could do:

另外,我应该提到,您可以在交集类型中拥有任意数量的类型。所以如果你愿意,你可以这样做:

public class MyWidget<T extends Enum<T> & MyInterface & Serializable & Cloneable> {
    ...
}

[Note: this code sample should not be construed as an endorsement of the Cloneableinterface; it was merely handy at the time.]

[注意:此代码示例不应被解释为对Cloneable接口的认可;它当时只是方便。]

回答by Alex Miller

The JSR 203 (new new IO) stuff for JDK 7 is making a lot of use of enums that implement interfaces (for example: http://openjdk.java.net/projects/nio/javadoc/java/nio/file/FileVisitOption.html) to allow them some wiggle room in the future for future additional sets of enum options. So that is a feasible approach and obviously one that was chosen after a lot of thought in one large Sun project.

JDK 7 的 JSR 203(新的新 IO)内容大量使用了实现接口的枚举(例如:http: //openjdk.java.net/projects/nio/javadoc/java/nio/file/FileVisitOption .html) 以允许他们在将来为将来的其他枚举选项集留出一些回旋余地。所以这是一种可行的方法,显然是在一个大型 Sun 项目中经过深思熟虑后选择的。