【发布时间】:2009-07-01 18:44:09
【问题描述】:
我不是 100% 相信这是一个好主意,但我今天遇到了一些代码,目前实现如下:
class MyWidget <T extends Enum<T> > {
MyWidget(Map<T, Integer> valueMap) {
mValueMap = valueMap;
}
Map<T, Integer> mValueMap;
}
其中MyWidget 然后提供使用mValueMap 将传入的Enum 转换为Integer 或从Integer 转换的方法。
我正在考虑做的是尝试重构它,以便我声明我的枚举:
interface MyInterface {
public Integer getValue();
}
enum MyEnum implements MyInterface {
foo, bar;
public Integer getValue() {
return ordinal();
}
}
然后我就可以将MyWidget 重写为看起来像这样的东西:
public class MyWidget<T extends Enum<T> extends MyInterface> {
...
}
然后就可以在MyWidget 内的T 类型对象上从MyInterface 调用getValue() 方法。当然,问题在于“<T extends Enum<T> extends MyInterface>”不是有效的语法。有没有办法解决这个问题?
我不想只有MyWidget<T extends MyInterface>,因为 T 是一个枚举也很重要。
提前致谢!
【问题讨论】: