我正在尝试使用扩展通用接口的枚举集合,如下所示:
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++一样使用它,所以如果我把它当作锤子而不是锯或者忽略某些东西,那么就可以轻松地指出它:)