枚举< ;?延伸界面> [英] Enum<? extends interface>
问题描述
interface Fooable
{
void someCommonMethod();
}
枚举E1实现Fooable
{
// someCommonMethod()的一些枚举和定义
}
枚举E2实现Fooable
{
// someCommonMethod()的一些不同的枚举和不同的定义
}
,然后在其他地方使用这个变量实现一个变量是Enum 和实现接口。所以一些东西沿着... ..
bar(Enum<?extends Fooable> fe)
{
fe.ordinal();
fe.someCommonMethod();
}
然而,到目前为止,我似乎不得不施展fe来对待它作为实现接口,即
bar(Enum<?extends Fooable> fe)
{
fe.ordinal();
((Fooable)fe).someCommonMethod();
}
虽然这应该是安全的...似乎次优,我可能俯瞰某物当然,如果我试图把param当成一个可乐,那么我会把它当作一个枚举,而不是这个没有收获我现在甚至不安全。请参阅以下内容:
bar(Fooable fe)
{
//潜在的不安全的投票!
((Enum<?>)fe).ordinal();
fe.someCommonMethod();
}
有没有什么我忽略或是
枚举<?扩展Fooable>
关于接近一个好的解决方案,我会得到?
我比较新的Java,我仍然在追求自己试图使用它像C或C ++,所以如果我把它像一把锤子,而不是一个锯子,或俯瞰一些愚蠢的简单,自由指出:)
您有一个选项是将您需要的Enum中的任何方法添加到Fooable或创建新界面扩展了Fooable,并添加了您需要的枚举方法。
示例:
interface Fooable {
void someCommonMethod();
}
interface FooableEnum extends Fooable {
int ordinal();
}
枚举E1实现FooableEnum {
//实现someCommonMethod。
// ordinal()已默认实现。
}
完成后,您可以使用 FooableEnum
作为您的方法签名中的参数类型,不用担心任何通用的东西。
I'm attempting to have a collection of enums that extend a common interface, so something like:
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()
}
and then make use of this elsewhere by enforcing both that a variable is a Enum and implements the interface. So something along the lines of..
bar(Enum<? extends Fooable> fe)
{
fe.ordinal();
fe.someCommonMethod();
}
However, so far I seem to have to cast fe in order to treat it as implementing the interface, i.e.,
bar(Enum<? extends Fooable> fe)
{
fe.ordinal();
((Fooable)fe).someCommonMethod();
}
and while this should be safe... it seems suboptimal and that I may be overlooking something. Of course if I try to just pass the param as a Fooable then I wind up casting to treat it as a Enum and not only is this no-gain I'm now not even safe. See following:
bar(Fooable fe)
{
// potentially unsafe cast!
((Enum<?>)fe).ordinal();
fe.someCommonMethod();
}
Is there anything I'm overlooking or is the
Enum<? extends Fooable>
about as close to a 'good' solution as I'll get?
I am relatively new to Java and am still catching myself trying to use it like C or C++ so if I'm treating it like a hammer instead of a saw or overlooking something stupidly simple feel free to point it out :)
One option you have is to add any of the methods from Enum you need onto Fooable or create a new interface that extends Fooable and adds the Enum methods you need.
Example:
interface Fooable {
void someCommonMethod();
}
interface FooableEnum extends Fooable {
int ordinal();
}
enum E1 implements FooableEnum {
// Implement someCommonMethod.
// ordinal() is already implemented by default.
}
Once you've done this you can use FooableEnum
as the parameter type in your method signature and not worry about any of the generic stuff.
这篇关于枚举< ;?延伸界面>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!