关于getClass的困惑

隐官城头坐 2013-11-07 08:39:54

public class SE extends Date{
public static void main(String[] args) {
new SE().test();
}
void test(){
System.out.println(super.getClass().getSimpleName());
}
}
谁能分析下为什么输出是SE,而不是Date
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
隐官城头坐 2013-11-07
  • 打赏
  • 举报
回复
引用 1 楼 rumlee 的回复:
getClass方法其实是从Object类继承类的,你不管调用谁的都是一样,所有的java类都不会去重写这个方法的。 Object类的getClass方法返回此 Object 的运行时类。
谢谢解答,让我懂了。楼下的说得好,应该看看Java帮助文档的
TKD03072010 2013-11-07
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
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。
遇到这种问题,看看API文档或源代码
  • 打赏
  • 举报
回复
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。
rumlee 2013-11-07
  • 打赏
  • 举报
回复
getClass方法其实是从Object类继承类的,你不管调用谁的都是一样,所有的java类都不会去重写这个方法的。 Object类的getClass方法返回此 Object 的运行时类。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧