public class SE extends Date{ public static void main(String[] args) { new SE().test(); } void test(){ System.out.println(super.getClass().getSimpleName()); } } 谁能分析下为什么输出是SE,而不是Date getclass
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。
Object类的getClass方法返回此 Object 的运行时类。
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。
谢谢解答,让我懂了。楼下的说得好,应该看看Java帮助文档的