I'm not 100% convinced that this is a good idea, but I bumped into some code today that's currently implemented as:
class MyWidget <T extends Enum<T> > {
MyWidget(Map<T, Integer> valueMap) {
mValueMap = valueMap;
}
Map<T, Integer> mValueMap;
}
where MyWidget
then offers methods that use mValueMap
to convert the passed-in Enum
to/from an 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 MyWidget
into something that looked vaguely like this:
public class MyWidget<T extends Enum<T> extends MyInterface> {
...
}
and would then be able to call the getValue()
method from MyInterface
on 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?
I don't want to just have MyWidget<T extends MyInterface>
, because it's also important that T be an enumeration.
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…