枚举<?扩展接口>

Kha*_*ots 9 java enums interface type-constraints

我正在尝试使用扩展通用接口的枚举集合,如下所示:

interface Fooable
{
  void someCommonMethod();
}

enum E1 implements Fooable
{
   // some enumuerations and a definition for someCommonMethod()
}

enum E2 implements Fooable
{
   // some different enumerations and a different definition for someCommonMethod()
}
Run Code Online (Sandbox Code Playgroud)

然后通过强制执行变量是枚举实现接口来在其他地方使用它.所以有些东西......

bar(Enum<? extends Fooable> fe)
{
  fe.ordinal();
  fe.someCommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

但是,到目前为止,我似乎必须抛出fe以将其视为实现接口,即

bar(Enum<? extends Fooable> fe)
{
  fe.ordinal();
  ((Fooable)fe).someCommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

虽然这应该是安全的...它似乎不是最理想的,我可能会忽略某些东西.当然,如果我试图将param作为Fooable传递,那么我最终会将它作为一个Enum来对待它,这不仅仅是没有收获我现在甚至都不安全.见如下:

bar(Fooable fe)
{
  // potentially unsafe cast!
  ((Enum<?>)fe).ordinal();
  fe.someCommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

有什么我忽略或是

Enum<? extends Fooable>
Run Code Online (Sandbox Code Playgroud)

我能得到的接近"好"的解决方案?

我对Java比较陌生,我仍然试图像C或C++一样使用它,所以如果我把它当作锤子而不是锯或者忽略某些东西,那么就可以轻松地指出它:)

Håv*_*hus 20

这意味着T扩展了Enum并实现了Fooable:

<T extends Enum<T> & Fooable>
Run Code Online (Sandbox Code Playgroud)

因此,您的方法可以写成:

<T extends Enum<T> & Fooable> void bar(T fe) {
    fe.ordinal();
    fe.someCommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

  • @Khanmots [方法本身可以是通用的](http://docs.oracle.com/javase/tutorial/extra/generics/methods.html). (2认同)

Mik*_*eck 1

您可以选择的一种选择是将您需要的 Enum 中的任何方法添加到 Fooable 上,或者创建一个扩展 Fooable 的新接口并添加您需要的 Enum 方法。

例子:

interface Fooable {
   void someCommonMethod();
}

interface FooableEnum extends Fooable {
   int ordinal();
}

enum E1 implements FooableEnum {
   // Implement someCommonMethod.
   // ordinal() is already implemented by default.
}
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您可以将FooableEnum其用作方法签名中的参数类型,而不必担心任何通用内容。