Java实现接口的枚举类数组也接受未实现接口的其他枚举。

huangapple go评论83阅读模式
英文:

Java Array of Enum Class implementing an interface accepts other Enums not implementing Interfaces as well

问题

我有一个实现Direction接口的Java枚举,如下所示。

public enum AdvanceDirection implements Direction {....}

public enum BasicDirection implements Direction {....}

我还有一个不实现Direction接口的枚举NonDirection,如下所示。

public enum NonDirection {....}

我有一个函数,应该只返回实现Direction接口的Enum类的数组,如下所示。

public static <T extends Enum<T> & Direction> Class<T>[] getDirections() {
  return new Class[]{AdvanceDirection.class, BasicDirection.class, NonDirection.class};
}

但是,如果我将NonDirection.class添加到这个数组中,它仍然返回它而不会出现任何错误。如何确保getDirections()方法只返回实现Direction接口的枚举数组?

用法示例:

public static void printDirections() {
  for (Class c: getDirections()) {
    System.out.println(c);
  }
}
英文:

I have java enums that implement a Direction interface as below.

public enum AdvanceDirection implements Direction {....}

public enum BasicDirection implements Direction {....}

I also have an enum NonDirection that doesn't implements Direction as below

public enum NonDirection {....}

I have a function that should only return an Array of Enum classes that implements Direction as below

public static &lt;T extends Enum&lt;T&gt; &amp; Direction&gt; Class&lt;T&gt;[] getDirections() {
  return new Class[]{AdvanceDirection.class, BasicDirection.class, NonDirection.class};
}

But if I add NonDirection.class to this Array, it still returns it without any errors. How can I make sure that getDirections() method only returns array of Enums that implements Direction Interface ?

Usage example

public static void printDirections() {
  for (Class c: getDirections()) {
    System.out.println(c);
  }
}

答案1

得分: 1

你遇到了Java中泛型使用的已知限制。语言设计者(在我看来是明智的)决定使用类型擦除来限制泛型对运行时引擎的影响。然而,这限制了一些本来看起来很自然的泛型用法。

你的选择有:

  1. 使用集合而不是数组。这将是我的建议:在你所提供的情况下,实际上没有数组的优势,而且会在编译时允许更严格的类型检查。

  2. 使用反射在运行时测试类的继承关系并抛出异常。

我意识到你的示例是为了提问而人为构造的(我对此表示赞赏),但你所展示的泛型和Class对象的使用似乎相当不寻常。如果你提供更多关于你试图实现的目标的细节,可能会有更自然的Java解决方案。

英文:

You have hit a known limitation of the use of generics in Java. The language designers (sensibly IMO) decided to use type erasure to limit the impact of the introduction of generics on the runtime engine. However this limits some uses of generics that would otherwise seem natural.

Your options are:

  1. use a collection instead of an array. This would be my recommendation: there's really no advantage to arrays in the case you've given and will allow tighter type checking at compile time.

  2. use reflection to test the class's inheritance at runtime and throw an exception.

I realise your examples is contrived for the purpose of asking the question (for which I commend you) but the use of generics and Class objects you've shown seems quite unusual. If you give more details on what you are trying to achieve there could well be a more natural solution within Java.

huangapple
  • 本文由 发表于 2020年8月8日 06:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63309657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定