super getClass()
首先看一段代码:
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 Superclass
, Subclass
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()的更多相关文章
- super.getClass()方法调用
下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...
- Java的super调用案例: super.getClass()返回的是子类自己
If you override a method from your superclass (or your superclass's superclass etc.), super.theMetho ...
- super.getClass()方法
下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...
- super.getClass()与this.getClass()
原文地址:http://leihuang.org/2014/11/14/getClass-method/ 首先看一段代码: import java.util.Date; public class Te ...
- 【Java面试题】14 super.getClass()方法调用
下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...
- super.getclass()的结果是父类还是子类?
package as; import java.util.Date; public class Test extends Date{ public static void main(String[] ...
- Java基础面试题:super.getClass().getName() 执行结果是什么?
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- this.getClass()和super.getClass()得到的是同一个类
今天dubug代码时发现this.getClass()和super.getClass()得到的竟然是同一个类,都是当前类. 遍访网络资料得出: getClass()不受this和super影响,而是有 ...
- super.getClass()方法调用?
下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...
随机推荐
- 从指定的URL下载文件
通过使用URLDownLoadToFile函数,我们能从指定的URL下载文件,保存到本地,并且下载的文件类型可以是可执行文件 实例如下,http://www.xuexic.com 的根目录下存在一个l ...
- Excel替换应用
* 是通配符 ,代表任意字符 ?也是通配符, 代表单个字符替换回车 : 按住alt键不放,然后依次通过数字键盘输入1和0两个数字,放开数字键后再放开alt键.
- Html中代码换行造成空格间距的问题
Html中代码换行造成空格间距的问题解析 解决方法: 一.简单粗爆不换行 写代码的时候不要换行,input等在一行输写,那么将解决该问题.但是代码就变得不再那么容易好看. 二.设置父级块的字体大小为0 ...
- 【JavaScript忍者秘籍】定时器
- robotframework接口测试初探1
robotframework这个框架最近很多人在使用它,大部分是和selenium结合的,大概看了下,然后发现这个做接口测试感觉也还不错,初步研究了下 环境安装: robotframework这个环境 ...
- RealProxy深入
Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...
- Github注册账户过程
首先打开网站https://github.com/ 第一个是输入用户名第二个是邮箱第三个是自己的密码然后点 sign up for github 此时会有个邮箱注册的验证进入注册的邮箱点击认证. ...
- vs2010配置boost编程环境(照抄并简化)
第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_50_0.7z. 第二步:在D盘(我C盘空间不大,准备出至少10G的空间吧)创建 ...
- 谈事件冒泡(Bubble)和事件捕捉(capture)
事件的发生顺序 假设在一个元素中又嵌套了另一个元素并且两者都有一个onClick事件处理函数(event handler).如果用户单击元素2,则元素1和元素2的单击事件都会被触发.但是哪一个事件先被 ...
- PowerBuilder 简介及应用 - 数据库系统原理
PowerBuilder 是一种企业级数据库前端应用和多层体系结构开发工具,友好的用户界面,功能强大的数据窗口,是一个集成开发环境. PB 的特点 采用面向对象的编程方法和事件驱动的工作原理. 支持跨 ...