为了讲解这个问题,我们先来看一下下面的代码:

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()的讲解的更多相关文章

  1. 关于java中assert(断言)的使用讲解

    说明:写的不是很全面,有任何问题请留言,多交流,谢谢! 1.eclipse.myeclipse开启assert(断言),默认是关闭,如下: 说白了就是设置一下jvm的参数,参数是-ea或者-enabl ...

  2. Java中com.jcraft.jsch.ChannelSftp讲解

    http://blog.csdn.net/allen_zhao_2012/article/details/7941631 http://www.cnblogs.com/longyg/archive/2 ...

  3. Java中的intern变量的讲解

    一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...

  4. 有关java中的try{}catch(){}的讲解

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_38225558/article/d ...

  5. java中Class对象详解和类名.class, class.forName(), getClass()区别

    一直在想.class和.getClass()的区别,思索良久,有点思绪,然后有网上搜了搜,找到了如下的一篇文章,与大家分享. 原来为就是涉及到Java的反射----- Java反射学习 所谓反射,可以 ...

  6. java中的getClass()函数

    Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...

  7. java中instanceof和getClass()的作用

    初学者难免有点混淆java中instanceof和getClass()的作用,  下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { }  构造对象 ...

  8. java中反射讲解及实例

    Java反射机制详解 java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实 ...

  9. java中为什么不能通过getClass().getName()获取父类的类名

    例如: class A{} public class B extends A{ public void test(){ System.out.println(super.getClass().getN ...

随机推荐

  1. juniper常用命令(二)

    Juniper防火墙基本命令 常用查看命令 Get int查看接口配置信息 Get int ethx/x查看指定接口配置信息 Get mip查看映射ip关系 Get route查看路由表 Get po ...

  2. firefox和chrome实现页面打印自动分页

    在Firefox和chrome中直接调用打印功能的js方法是 window.print(); 但是如果页面很长,那么就需要分页,这时只需要在页面中添加css属性即可,如果想自动分页,则如下所示 < ...

  3. linux基础(8)-文件处理(awk 、sed、grep)

    grep基本用法 格式:grep [选项] [模式] [文件]   选项: -c:只显示有多少行匹配 ,而不具体显示匹配的行 -n:在每一行前面打印该行在文件中的行数 -i:在字符串比较的时候忽略大小 ...

  4. 利用虚拟网桥实现Docker容器的跨主机访问

    最近在研究Docker,Docker的网络配置是比较令人头疼的部分,尤其是跨主机的容器间通信,很多解决方案都比较复杂,这里,我只用虚拟网桥来实现Docker的跨主机访问,分享出来,希望对Docker学 ...

  5. 智课雅思词汇---二十、前缀syn-sym-syl是什么意思

    智课雅思词汇---二十.前缀syn-sym-syl是什么意思 一.总结 一句话总结:l,m,n是可以互换 前缀:sy-, syn-, sym-, syl- [词根含义]:共同,同时 [词根来源]:(s ...

  6. MRC与ARC混合编程的编译器标记

    如果是MRC项目创建ARC源文件,给这个源文件加上 -fobjc-arc 的编译器标记, 如果是ARC项目创建MRC源文件,给这个源文件加上 -fno-objc-arc 的编译器标记. 步骤: 1. ...

  7. 如何关闭 window10 自带的杀毒软件

    打开运行,快捷键[win+R],输入[ gpedit.msc],点击确定 在打开的组策略窗口中,依次展开计算机配置>管理模板>Windows组件,选择[windows组件]中的window ...

  8. Maven下载 || 配置本地仓库 || IntelliJ IDEA配置Maven教程

    本文章主要介绍1.Maven下载   2.配置本地仓库Repository   3.IDEA配置Maven 三点. 相关博客: Eclipse配置Maven https://www.cnblogs.c ...

  9. 22 Python 模块与包

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  10. 配合Jenkins自动化构建,bat脚本(一)

    C:\Windows\System32\inetsrv\appcmd.exe stop site ServiceIIS C:\Windows\System32\inetsrv\appcmd.exe s ...