参数如何传入带泛型的 class

364 天前
 CHEN1016
方法有个参数需要传入一个 class ,但是这个 class 也是带了泛型的,要怎么传入呢
1356 次点击
所在节点    Java
5 条回复
wolfie
364 天前
public static <T, F> void method(Class<T> foo, Class<F> bar) {}
XiaoXiaoMagician
364 天前
直接贴代码片段或例子把,你的文字没办法呈现出你的具体问题。
CHEN1016
364 天前
@XiaoXiaoMagician <T> ResponseEntity<T>
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables)比如这个 resttemplate 的一个方法,responseType 要传一个 class ,但是我想返回的类是带泛型的
mmdsun
364 天前
你发的代码是 Spring 的?是不是这种? ParameterizedTypeReference 是 Spring 的类,处理泛型擦除的

ResponseEntity<List<MyClass>> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<MyClass>>() {}
);
List<MyClass> myObjects = responseEntity.getBody();
x77
364 天前
参数类型用 Object ,自己写代码转类型,Java 泛型功能不完善,不能依赖它的泛型检查。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/953087

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX