首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

奇怪有关问题

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

奇怪问题?
请教高手,   为什么下面程序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中提供的,

热点排行
Bad Request.