首先看一段代码:

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. asp.net fileupload上传大文件时提示404.13错误

    IIS 7 默认文件上传大小时30M 要突破这个限制,需要做如下操作: 1. 修改IIS的applicationhost.config     打开 %windir%\system32\inetsrv ...

  2. 基于Maven site的穷人的本地知识管理系统

    1 Motivation On daily study or development, a simple knowledge management system is required. In the ...

  3. tab切换效果

    选项卡是一个神奇的网页效果,不论大小网站,比如B2B像阿里巴巴,慧聪网,还有B2C这个不用说了吧,爱逛网店的童鞋们都知道的,像京东商城,淘宝网,拍拍网,一号店,凡客诚品,等等各种网各种网店,选项卡不仅 ...

  4. 用sql语句建表

    CREATE TABLE USER (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30) NOT NULL, p ...

  5. MySQL5.7.11安装

    1.官网下载mysql-5.7.11-winx64.zip 2.将压缩包解压至D:\Program Files,配置环境变量

  6. web view调h5的方法死活调不到

    (WebViewJavascriptBridge注册方法都能调用,只有callhandlename时无论如何也没响应)这个问题真是纠缠我好久了 webview评论区刷新问题终于找到原因了 ,我试着把咨 ...

  7. 初识Java-分数录入系统

    package classTest; import java.util.Scanner; public class scoreArrangement { /**  * 选择界面(main)  */ p ...

  8. linux笔记_防止ddos攻击

    一.什么是DoS攻击 DoS是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务.最常见的DoS攻击有计算机网络带宽攻 ...

  9. [NOIP2011] 聪明的质检员(二分答案)

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...

  10. 取得系统属性和Java环境

    代码如下: import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.uti ...