请教高手, 为什么下面程序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()); }
}
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()); }
}
是 this.t.getName();星期一好晕啊, 呵呵
this.t.getClass().getName();这样能点出来?
如果你将Class 改成 Object 就可以输出你想要的结果了,
注意Object 是所有类的超类,而Class 不是
getClass()只是在Object中提供的,