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[ ...
随机推荐
- android 瀑布流效果 保存地址
http://tech.ddvip.com/2013-09/1379785198203013_2.html
- 事件委托能够优化js性能
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [翻译]Primer on Cognitive Computing(认知计算入门)
Source Kelly J., Primer on Cognitive Computing 20150216. 侵删,联系方式:zhoujiagen\@gmail.com. 按A candidate ...
- CSS应用心得
单纯Html配合CSS网页 下面用程序来实际总结一下 首先,在写程序的应该具有一个良好的编程习惯. 第一:排版,拥有一个良好的排版,有助于我们能够快速的理解以及阅读程序: 第二:注释,就如以下程序,作 ...
- ArrayList转成HashMap再转成LinkedHashMap 自己的解决方案
做天津杰超项目中赛事活动作品审核中写的一段代码: //获取全部作品 ActivityProductionQueryCommond productionQueryCommond=new Activity ...
- 《java编程思想》读书笔记(二)第五章(2)
成员初始化 Java尽力保证:所有变量在使用前都能得到恰当的初始化. 对于方法的局部变量,Java会以编译时报错的形式贯彻这种保证,比如: void f() { int i; //这里编译时就会报错, ...
- ffmpeg音频编码
在弄音频采集时,需要设置缓存的大小,如果只是简单的采集和直接播放PCM数据,缓存的大小一般不影响播放和保存. 但是,如果需要使用FFMpeg音频编码,这时,音频缓存的大小必须设置av_samples_ ...
- 关系与导航属性(摘自微软MSDN)
关系与导航属性 本主题概述实体框架如何管理实体间的关系.还对如何映射和操作关系提供了一些指南. 关系.导航属性和外键 在关系数据库中,表之间的关系(也称为关联)是通过外键定义的.外键 (FK) 是用于 ...
- iOS_Swift初识之使用三种回调方式自定义Button
最近在学习Swift ,发现青玉伏案大神早期用OC写的一篇博客--IOS开发之自定义Button(集成三种回调模式) 很适合用来熟悉Swift的回调方式,于是我就用Swift翻版了一下,具体实现原理 ...
- 初遇 dotcloud
逛园子的时候看到新浪SAE,正学习建站呢,好东西.(论环境的影响...) 不过发现新浪SAE只支持 Python2,我更喜欢 Python3 e...找找其他的,发现了 dotcloud,遂试试,下面 ...