java Java泛型问题:类型参数E不在其范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4775365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Generics Question: type parameter E is not within its bound
提问by Kraushauslaus
i've got a question about generics. I have this method which does not compile at all. The compiler tells me: type parameter E is not within its bound
. I've usually no problem in understanding compiler errors, but this one is quite tricky. Maybe my knowledge about generics need to improve. :-) Can anyone tell me whats wrong?
我有一个关于泛型的问题。我有这种根本无法编译的方法。编译器告诉我:type parameter E is not within its bound
. 我通常在理解编译器错误方面没有问题,但这个问题非常棘手。也许我对泛型的了解需要提高。:-) 谁能告诉我出了什么问题?
public static <E extends Enum & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) {
Map<String, E> mapping = new HashMap<String, E>();
EnumSet<E> set = EnumSet.allOf(enumClass);
for(E enumConstant : set) {
mapping.put(enumConstant.getStringValue(), enumConstant);
}
return mapping;
}
This is the definition of StringConvertableEnum
:
这是 的定义StringConvertableEnum
:
public interface StringConvertableEnum<E extends Enum> {
public E getEnumFromStringValue(String string);
public String getStringValue();
}
回答by Yishai
You need to change your declaration to E extends Enum<E>
您需要将您的声明更改为 E extends Enum<E>
Edit, sorry had to step away, the full declaration I mean is:
编辑,抱歉不得不走开,我的意思是完整的声明是:
public static <E extends Enum<E> & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) {