问题描述
我有一个关于泛型的问题。我有这种方法,根本不编译。编译器告诉我:类型参数E不在它的边界内
。我通常在理解编译器错误方面没有问题,但是这个问题非常棘手。也许我对泛型的知识需要改进。 :-)谁能告诉我什么是错的?
Map< String,E> mapping = new HashMap< String,E>();
EnumSet< E> set = EnumSet.allOf(enumClass); (E enumConstant:set)
{
mapping.put(enumConstant.getStringValue(),enumConstant);
}
返回映射;
$ b
这是 您需要更改声明到 编辑,抱歉不得不离开,我的意思是完整的声明是: i've got a question about generics. I have this method which does not compile at all. The compiler tells me: This is the definition of You need to change your declaration to Edit, sorry had to step away, the full declaration I mean is: 这篇关于Java泛型问题:类型参数E不在其范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! StringConvertableEnum $ c的定义$ c $:
pre $ public interface StringConvertableEnum< E extends Enum> {
public E getEnumFromStringValue(String string);
public String getStringValue();
E扩展枚举< E>
public static< E extends Enum< E> &安培; StringConvertableEnum< E - 代替;>地图< String,E> map(Class< E> enumClass){
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?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;
}
StringConvertableEnum
:public interface StringConvertableEnum<E extends Enum> {
public E getEnumFromStringValue(String string);
public String getStringValue();
}
E extends Enum<E>
public static <E extends Enum<E> & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) {