[英]Java generic class as parameter
I have a basic question regarding Java generics. 我有一个关于Java泛型的基本问题。 I have a method that takes this as an argument: 我有一个方法,以此作为参数:
public id findKey(List<String> ids, CustomClass clazz) {
repos.findID(ids, clazz);
}
I then want to use a java generic class as an argument for another method: 然后,我想使用java泛型类作为另一种方法的参数:
repos.findID(ids, clazz);
but my findID method needs a java generic class instead of clazz. 但是我的findID方法需要一个Java通用类而不是clazz。 How do I get my clazz argument from the findkey parameter and turn it to a generic class? 如何从findkey参数获取clazz参数并将其转换为通用类? If I do this: 如果我这样做:
public id findKey(List<String> ids, Class<T> clazz) {
repos.findID(ids, clazz);
}
I get an error because it won't recognize my T. 我收到一个错误,因为它无法识别我的T。
I'm at loss at how this stuff works and would appreciate any help. 我对这些东西的工作方式一无所知,将不胜感激。
Edit: Solved it. 编辑:解决了。 I can use ? 我可以用 ? or explicitly state a class type that I wanted and it accepted it. 或明确声明我想要的类类型并接受它。
There are two options: 有两种选择:
define your type as unknown 将您的类型定义为未知
public id findKey(List<String> ids, Class<?> clazz)
define a type variable 定义类型变量
public <T> id findKey(List<String> ids, Class<T> clazz)
使用public id findKey(List<String> ids, Class<?> clazz)
并了解有关通配符的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.