关于java中getClass()和getSuperClass()的讲解
为了讲解这个问题,我们先来看一下下面的代码:
package com.yonyou.test;
import java.util.Date;
class Test extends Date{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new Test().print();
}
public void print(){
System.out.println("当前运行类的名字为:"+super.getClass().getName());
System.out.println("当前运行类的名字为:"+this.getClass().getName());
System.out.println("当前运行类的继承的父类的名字为:"+this.getClass().getSuperclass().getName());
}
}
上面代码的输出结果什么样的呢?
也许你需要上机调试一下,因为有些不确定,下面我们就一起来分析一下:
上机调试后发现运行的结果为:
当前运行类的名字为:com.yonyou.test.Test
当前运行类的名字为:com.yonyou.test.Test
当前运行类的继承的父类的名字为:java.util.Date
先来分析一下getClass()究竟返回的是什么:
插卡jdk的源码可以看到如下内容:
/**
* Returns the runtime class of this {@code Object}. The returned
* {@code Class} object is the object that is locked by {@code
* static synchronized} methods of the represented class.
*
* <p><b>The actual result type is {@code Class<? extends |X|>}
* where {@code |X|} is the erasure of the static type of the
* expression on which {@code getClass} is called.</b> For
* example, no cast is required in this code fragment:</p>
*
* <p>
* {@code Number n = 0; }<br>
* {@code Class<? extends Number> c = n.getClass(); }
* </p>
*
* @return The {@code Class} object that represents the runtime
* class of this object.
* @see <a href="http://java.sun.com/docs/books/jls/">The Java
* Language Specification, Third Edition (15.8.2 Class
* Literals)</a>
*/
public final native Class<?> getClass();
一定要看上面注释中的蓝色部分的英文注释,意思是返回对应的当前正在运行时的类所对应的对象,那么super可以理解为Test的父类Date,
那么请问当前Date类正在运行时的对象是谁?没错,就是其子类Test,所以this.getClass(0.getName()和super.getClass().getName()返回的
都是com.yonyou.test.Test
再看看getSuperClass()的源码
/**
* Returns the <code>Class</code> representing the superclass of the entity
* (class, interface, primitive type or void) represented by this
* <code>Class</code>. If this <code>Class</code> represents either the
* <code>Object</code> class, an interface, a primitive type, or void, then
* null is returned. If this object represents an array class then the
* <code>Class</code> object representing the <code>Object</code> class is
* returned.
*
* @return the superclass of the class represented by this object.
*/
public native Class<? super T> getSuperclass();
没错,这里蓝色标注的才是返回当前实体类的父类。所以要返回当前类的父类的话,请使用下面这中方式
super.getClass().getSuperClass().getName();
关于java中getClass()和getSuperClass()的讲解的更多相关文章
- 关于java中assert(断言)的使用讲解
说明:写的不是很全面,有任何问题请留言,多交流,谢谢! 1.eclipse.myeclipse开启assert(断言),默认是关闭,如下: 说白了就是设置一下jvm的参数,参数是-ea或者-enabl ...
- Java中com.jcraft.jsch.ChannelSftp讲解
http://blog.csdn.net/allen_zhao_2012/article/details/7941631 http://www.cnblogs.com/longyg/archive/2 ...
- Java中的intern变量的讲解
一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...
- 有关java中的try{}catch(){}的讲解
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_38225558/article/d ...
- java中Class对象详解和类名.class, class.forName(), getClass()区别
一直在想.class和.getClass()的区别,思索良久,有点思绪,然后有网上搜了搜,找到了如下的一篇文章,与大家分享. 原来为就是涉及到Java的反射----- Java反射学习 所谓反射,可以 ...
- java中的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- java中instanceof和getClass()的作用
初学者难免有点混淆java中instanceof和getClass()的作用, 下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { } 构造对象 ...
- java中反射讲解及实例
Java反射机制详解 java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实 ...
- java中为什么不能通过getClass().getName()获取父类的类名
例如: class A{} public class B extends A{ public void test(){ System.out.println(super.getClass().getN ...
随机推荐
- Centos6.5安装glusterfs3.6.2
硬件环境Centos6.5 glusterfs3.6.2 先安装必要的包 yum install flex bison 2. 下载glusterfs3.6.2 wget http://downloa ...
- struts2中常用配置
1.Post提交乱码问题,如果编码采用的是utf-8,那么默认不需要自己处理,因为其默认的常量配置文件就是处理UTF-8的 这个常量值只处理POST提交,get如果乱码还得自己写拦截器处理,一般只要页 ...
- js实现级联菜单(没有后台)
html代码: <!-- js级联菜单 --> <div id="cascMenu"> <select id="select" o ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...
- 初始化dataframe
由字典生成dataframe: >>> d = {'col1': [1, 2], 'col2': [3, 4]} >>> df = pd.DataFrame(dat ...
- MDX中Filter 与Exist的区别
获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...
- The import java.util cannot be resolved
原因:项目buildpath不对 解决方案: 右键项目-------build path--------configuration build path 的选择libraries找到JRE(这个时候你 ...
- OS X 10.11 El Capitan 三指拖动的开启方法
触摸板的三指拖动功能已经被移到“辅助功能”中. 开启步骤: 系统偏好设置-辅助功能-鼠标与触摸板,点击下面的“触摸板选项...”按钮,勾选“启用拖拽”并且从下拉列表里选择“三指拖移” .
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
- tableau 常识积累
没怎么在业务系统中使用过,所以需要好好积累.看起来很简单的东西都需要慢慢来用.下了一份它的官方文档10.3版本的.公司网络限制,不能去它官网学习.只有下班时间了. 先说一个,有时候度量值它和HANA类 ...