日期:2014-05-20  浏览次数:20646 次

奇怪问题?
请教高手,   为什么下面程序this.t.getClass.getName()   输出为
java.lang.Class
而不是   TestGeneric啊?
谢谢!


//   TestGeneric.java
class   T2   {
Class   t;
public   T2(Class   c)   {
t   =   c;

}

public   String   toString()   {
return   this.getClass().getName()   +   "\n "   +   this.t.getClass().getName();
}
}

public   class   TestGeneric   extends   JButton   {
public   static   void   main(String[]   args)   throws   Exception   {
T2   t2   =   new   T2(TestGeneric.class);

System.out.println(t2.toString());

}
}




------解决方案--------------------
mark
------解决方案--------------------
倒塌````
this.t.getClass().getName();

这样能点出来?
------解决方案--------------------
你在 T2 t2 = new T2(TestGeneric.class); 这个语句中使用Object.class 返回的是一个Class类型的对象,这个对象的类 是 java.lang包下的Class 而不是TestGeneric。
如果你将Class 改成 Object 就可以输出你想要的结果了,
注意Object 是所有类的超类,而Class 不是
getClass()只是在Object中提供的,