读书人

奇怪有关问题

发布时间: 2012-01-19 00:22:28 作者: rapoo

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

读书人网 >J2SE开发

热点推荐