我見過類似的問題,但他們沒有太大的幫助。如何在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