public class SE extends Date{
public static void main(String[] args) {
new SE().test();
}
void test(){
System.out.println(super.getClass().getSimpleName());
}
}
谁能分析下为什么输出是SE,而不是Date
...全文
1094打赏收藏
关于getClass的困惑
public class SE extends Date{ public static void main(String[] args) { new SE().test(); } void test(){ System.out.println(super.getClass().getSimpleName()); } } 谁能分析下为什么输出是SE,而不是Date
super.getClass().getSuperclass().getSimpleName()
getClass():Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
getSuperclass():Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then the Class object representing the Object class is returned.
这样获取的是父类的名字。多看api。