我见过类似的问题,但他们没有太大的帮助。如何在Java中获取类型变量的类型泛型
比如我有这个泛型类:
public class ContainerTest<T>
{
public void doSomething()
{
//I want here to determinate the Class of the type argument (In this case String)
}
}
,并使用这种容器类
public class TestCase
{
private ContainerTest<String> containerTest;
public void someMethod()
{
containerTest.doSomething();
}
}
是否有可能确定的类型参数的类的方法DoSomething的另一类()没有明确类型变量/字段或任何构造函数在ContainerTest类?
更新:的ContainerTest类
'if(t instanceof String)'? – vikingsteve 2013-02-26 08:41:40
我已经更新了这个问题 – ZeDonDino 2013-02-26 08:44:48
你不能只将类类型作为参数传递吗? – 2013-02-26 08:47:05