gpt4 book ai didi

java - 泛型 - 如何返回 T 的 Map?

转载 作者:行者123 更新时间:2023-12-02 11:13:55 25 4
gpt4 key购买 nike

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 的 Map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50421668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com