【发布时间】:2015-08-28 21:04:12
【问题描述】:
我想知道 SO 上的 Java 大神是否有任何技巧可以分享如何完成以下工作
public class MyClass<T> {
public List<T> getMyList(Class1 a, String, b) {
Generic1<Generic2<T>, Class1> x = new Generic3<T>();
x.doSomething();
// this compiles but x doesn't work correctly since (of course) T is now type "Object"
}
}
//... calling it like this:
MyClass<MyType> c = new MyClass<>();
对于上面的对象“x”要完成它的工作,它需要知道 T 的类型是什么。 Generic1、Generic2 和 Generic3 不是我写的类。但是有什么方法可以传达类型信息以便它们起作用吗?说,如果我在运行时传入该类型的Class?
谢谢。
【问题讨论】:
-
"Any way to get around type erasure in Java?"-- 使用 C#? -
Avoiding Java Type Erasure 的可能重复项
-
告诉我们更多关于
x的信息。 -
x 在这种情况下是 JpaCriteriaQueryVisitor
() 的一个实例......我在上面称之为 Generic3。 -
请为您的问题提供MCVE...就目前而言,您的问题非常模糊,无法真正回答。
标签: java generics type-erasure