Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我想创建一个类,然后实例化一个实例变量,如下所示。我的问题是我用什么替换 Example.class 以使其通用 T.class 不起作用,Class 也不起作用。
public class MyClass<T> { public CurstomClass<T> customClass = new CustomClass<T>(Example.class); }
在 Java 中只有一种方法可以做到 - 将泛型类型类作为参数传递:
public class MyClass<T> { public final CustomClass<T> customClass; public MyClass( Class<T> clazz) { customClass = new CustomClass<T>(clazz); } }