也是无意间在论坛中发现的一个问题,就像一个脑筋急转弯。题目如下:
public class b1 extends Date {
public static void main(String[] args){
new b1().test();
}
public void test(){
System.out.println(super.getClass().getName());
}
}
这个东西结果是什么。
初看这东西,我只能说,输出:Date。但亲自敲代码试了后,发现输出b1。
是啥原因呢?
原来getClass()返回的是当前运行类的class名字,当前运行的类当然不会是Date了。所以super.getClass().getName()就相当于getClass().getName()。故输出为b1。
如果想得到父类classname。用getClass().getSuperclass().getName()即可。