我在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!!!");
Run Code Online (Sandbox Code Playgroud)
请告诉我为什么我没有得到ClassCastException,而且,在Idea中我看到temp String在赋值后具有值"Crack!!!".另外,我怎么能抛出ClassCastException?我在Windows 7 x64上使用JDK 1.7.0_07.