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[ ...
随机推荐
- tinyhttpd源码分析
我们经常使用网页,作为开发人员我们也部署过httpd服务器,比如开源的apache,也开发过httpd后台服务,比如fastcgi程序,不过对于httpd服务器内部的运行机制,却不是非常了解,前几天看 ...
- js中 ||的意思,js中 o = o || {};是什么意思呢?
o = o || {} 表示:如果o为null或undefined,则将o初始化空对象(即{}),否则o不变.目的是防止o为null或未定义的错误. 其中:||表示或操作,第一个条件为真,则结果为真而 ...
- Mac MySQL 转移 datadir
mysql默认的datadir在启动盘上面,有时database太大,于是决定将datadir迁到存储盘中 Step 1 将原datadir迁到存储盘 mv /usr/local/var/mysql ...
- Android App Build System
- mysql 操作用户权限
使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','password');如 ...
- Eclipse 中 Tomcat启动卡100%(preparing launch delegate...)
我自己遇到这个问题的时候去百度了好几天,没找到我的解决方案,因为我的错误和别人不一样,但提示却和别人一样,在tomcat启动到100%的时候,卡住了,最后显示45秒不够启动,建议我增加时间,所以结果可 ...
- 2016HUAS_ACM暑假集训4B - 递推
这种数学推理题目题意极其明显,在做的时候,可以多写几组,这样找起规律来会容易些.概括起来就是:题意简单暴力,案例毫无价值. 一个三角形最多可以把一个平面分成两部分,两个三角形最多是8(2+6)部分,而 ...
- 【HDU1257】最少拦截系统(贪心)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Postgresql FATAL: could not create semaphores: No space left on device
昨天安装完成pg 9.5后,启动报错: FATAL: could not create semaphores: No space left on device DETAIL: Failed sys ...
- python fork 用法
import os import sys ips = ( "192.168.45.%s" % i for i in range(1,255)) for ip in ips: pid ...