class MyObject{}
class Human extends MyObject{}

public ConcurrentSkipListMap<String, <T extends MyObject>> get(Class<T> clazz, Object... vars){
    //...
}

//use the get() with
ConcurrentSkipListMap<String, Human> mapOfAllAdams = get(Human.class, "Adam");


但是,返回值ConcurrentSkipListMap<String, <T extends MyObject>>给出编译错误,但是<T extends MyObject>T可以。我应该写些什么呢?

最佳答案

进行这些更改

class MyObject{}
class Human extends MyObject{}

public <T extends MyObject> ConcurrentSkipListMap<String, T> get(Class<T> clazz, Object... vars){
    //...

    return null;
}

//use the get() with
void hello(){
    ConcurrentSkipListMap<String, Human> x = get(Human.class, "Adam");
}


我在返回类型之前声明了<T extends MyObject>,还添加了一个变量x来保存get函数的返回值。

关于java - 泛型-如何返回T的图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50421668/

10-13 07:04