gpt4 book ai didi

java - 如何从参数化参数中获取参数的类型

转载 作者:行者123 更新时间:2023-12-01 15:19:39 25 4
gpt4 key购买 nike

我在尝试寻找解决方案(如果有的话)时遇到了麻烦:

public class Generics {
Map<Class<? extends SomeObject1>, SomeObject2>> map;
map = new HashMap<Class<? extends SomeObject1>, SomeObject2>>();

public static <E extends SomeObject1> SomeObject2 get(Class<E> c) {
if (map.containsKey(c))
return map.get(c);
else {
SomeObject2 o = new SomeObject2();
map.put(c, o);
return o;
}
}
}
...
//somewhere
public <T extends SomeObject1> void aMethod(AnInterestedClass<T> list) {
// How to get the value from the map
// knowing that the key is of type T?
Generics.get();
}

想法?

最佳答案

因为type erasure ,您只能通过将 Class 对象传递给 aMethod 来做到这一点。请参阅this related thread .

关于java - 如何从参数化参数中获取参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11133693/

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