【发布时间】:2013-05-17 08:52:21
【问题描述】:
我在 Java 中遇到了一个我完全无法理解的泛型行为(以我的 .NET 背景)。
public class TestGeneric<T>
{
public void get (Object arg)
{
T temp = (T) arg;
System.out.println(temp.toString());
return;
}
}
TestGeneric<Integer> tg = new TestGeneric<Integer>();
tg.get("Crack!!!");
请告诉我为什么我在 get 中没有得到 ClassCastException,此外,在 Idea 中,我在分配后看到 temp 为 String 并且具有 "Crack!!!" 的值。另外,我怎么能抛出 ClassCastException 呢?我在 Windows 7 x64 上使用 JDK 1.7.0_07。
【问题讨论】: