首先看一段代码:

import java.util.Date;
public class Test extends Date{
public static void main(String[] args) {
new Test().test();
}
public void test(){
System.out.println(super.getClass().getName());
}
}

上面这段代码的输出为:Test

可能你会奇怪为什么输出的是Test,而不是Date呢?我明明是调用的super.getClass()啊。我们先不急,先来了解一下getClass()方法的机制是什么。以下时getClass()在Object类中实现的源代码。

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. The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment: Number n = 0; Class<? extends Number> c = n.getClass(); Returns: The Class object that represents the runtime class of this object.                    getClass()方法: 返回此 Object 的执行时类。

public final native Class<?> More ...getClass();  getClass()是final方法。子类无法重写。

关键是理解super的含义:

http://docs.oracle.com/javase/tutorial/java/IandI/super.html

Using the Keyword super

Accessing Superclass Members

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:

public class Superclass {

    public void printMethod() {
System.out.println("Printed in Superclass.");
}
}

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {

    // overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from SuperclassSubclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:

Printed in Superclass.
Printed in Subclass

使用super来引用 被子类覆盖的父类的方法。跟父类对象没有关系。

用来引用一个方法而已。

super getClass()的更多相关文章

  1. super.getClass()方法调用

    下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...

  2. Java的super调用案例: super.getClass()返回的是子类自己

    If you override a method from your superclass (or your superclass's superclass etc.), super.theMetho ...

  3. super.getClass()方法

    下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...

  4. super.getClass()与this.getClass()

    原文地址:http://leihuang.org/2014/11/14/getClass-method/ 首先看一段代码: import java.util.Date; public class Te ...

  5. 【Java面试题】14 super.getClass()方法调用

    下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...

  6. super.getclass()的结果是父类还是子类?

    package as; import java.util.Date; public class Test extends Date{ public static void main(String[] ...

  7. Java基础面试题:super.getClass().getName() 执行结果是什么?

    package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...

  8. this.getClass()和super.getClass()得到的是同一个类

    今天dubug代码时发现this.getClass()和super.getClass()得到的竟然是同一个类,都是当前类. 遍访网络资料得出: getClass()不受this和super影响,而是有 ...

  9. super.getClass()方法调用?

    下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...

随机推荐

  1. [Beta] postmortem

    设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决网站前端的数据处理以及获取问题,定义的很清楚,对于典型用户也比较清晰,因为主要只有一个用户,所以对于 ...

  2. Jbrowse安装和序列、bam、vcf配置

    最近做了一个关于基因开发的项目,要求最终输出的文件可以在专门的基因浏览器上边显示,类似统计图的东西.废话不说上图(表示表达不出来0.0)! 先说下Jbrowse这个东西吧,一句话:一个简单的,便携式依 ...

  3. Win32隐藏输出console窗口

    #include <Windows.h> void HideConsole() { ::ShowWindow(::GetConsoleWindow(), SW_HIDE); } void ...

  4. 程序员写的东西出了bug,造成了损失谁来承担?

    这是个持续多年的话题了,很多大公司,尤其是牛逼的独立分包公司(开发公司)都会有代码审核和严格QA程序,一般的公司就很难说咯,在法律上目前还没有完全支持处罚程序员bug经济损失的判例(国内如此),国外也 ...

  5. Hello Docker (Docker 入门分享)

    Hello Docker 1 PPT @ http://download.csdn.net/download/liangread/9431056 TBD

  6. [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例

    LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6         ...

  7. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据

    1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现De ...

  8. windows server 2012 FTP SMB 文件夹权限继承

    被坑了..win默认的权限继承,有继承就没有smb目录继承,有smb目录继承 父级文件夹的权限就删不掉.. 换ftp轻松愉快...

  9. [BZOJ 2819]Nim

    最近都忙的没空写题解了喵- 看到 1= 终于是保住了也算是一个小小的安慰吧 555…… 湖北省队互测题,据说会爆栈,但 Linux 下 栈空间=内存=128M 真的吃不下? 反正我是写了个人工栈- 这 ...

  10. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...