本文介绍了Java泛型问题:类型参数E不在其范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个关于泛型的问题。我有这种方法,根本不编译。编译器告诉我:类型参数E不在它的边界内。我通常在理解编译器错误方面没有问题,但是这个问题非常棘手。也许我对泛型的知识需要改进。 :-)谁能告诉我什么是错的?

StringConvertableEnum< E - 代替;>地图< String,E> map(Class< E> enumClass){
Map< String,E> mapping = new HashMap< String,E>();

EnumSet< E> set = EnumSet.allOf(enumClass); (E enumConstant:set)

{
mapping.put(enumConstant.getStringValue(),enumConstant);
}

返回映射;



$ b

这是 StringConvertableEnum
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){


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?

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:

public interface StringConvertableEnum<E extends Enum> {
    public E getEnumFromStringValue(String string);
    public String getStringValue();
}
解决方案

You need to change your declaration to 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) {

这篇关于Java泛型问题:类型参数E不在其范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:44