直接上代码:
import java.util.Date;public class Super_Keyword extends Date { public static void main(String[] args) { Super_Keyword sk = new Super_Keyword(); sk.test(); } public void test() { System.out.println(this.getClass().getName()); System.out.println(super.getClass().getName()); } }输出结果如下:Super_Keyword
Super_Keyword
这里也许一不注意,就认为输出结果是Super_Keyword和Date。
这个getClass()实在 中定义的,其声明为了final native 。也就是说该方法是不能被子类覆盖的。所以this.getClass()与super.getClass()实际上都是调用的同一个方法,都是得到调用者的Class 对象。所以两行代码的输出都是 Super_Keyword。
如果想要得到父类的类名,需要使用this.getClass().getSuperClass().getName();
其实犯上面的错的原因就是不够仔细,被那个super给忽悠了。
继续阅读与本文标签相同的文章
下一篇 :
查看linux系统版本命令
-
Struts中forward与global-forward的区别
2026-06-02栏目: 教程
-
Struts 服务端表单数据验证流程
2026-06-02栏目: 教程
-
N个数中,1出现的次数
2026-06-02栏目: 教程
-
二进制形式中1出现的次数
2026-06-02栏目: 教程
-
什么是事务?
2026-06-02栏目: 教程
