关于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 ...
随机推荐
- Eclipse 换主题、皮肤、配色,换黑色主题护眼
Eclipse写android代码时,默认的文本和框架都是白色,长时间使用,显得过于刺眼.这里介绍三种方法换黑色护眼配色. 1.系统设置里更改 2.从Eclipse Marketplace里下载主题 ...
- Bluetooth Profile for iPhone from the functional perspectives
Classic Bluetooth Profile for iPhone from the functional perspectives Function Description BT Profil ...
- DL四(预处理:主成分分析与白化 Preprocessing PCA and Whitening )
预处理:主成分分析与白化 Preprocessing:PCA and Whitening 一主成分分析 PCA 1.1 基本术语 主成分分析 Principal Components Analysis ...
- <转载>获取运行中的TeamViewer的账号和密码
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <iostream> #pragma comment( li ...
- 直方图均衡化的 C++ 实现(基于 openCV)
这是数字图像处理课的大作业,完成于 2013/06/17,需要调用 openCV 库,完整源码和报告如下: #include <cv.h> #include <highgui.h&g ...
- 机器学习(六)—随机森林Random Forest
1.什么是随机采样? Bagging可以简单的理解为:放回抽样,多数表决(分类)或简单平均(回归): Bagging的弱学习器之间没有boosting那样的联系,不存在强依赖关系,基学习器之间属于并列 ...
- macvim/babun+spf13-vim
mac: macvim brew install macvim --with-cscope --with-lua --with-override-system-vim brew linkapps ma ...
- 时间服务器: NTP 服务器及客户端搭建
时间服务器: NTP 服务器及客户端搭建 一. NTP 服务器的安装与设定 1. NTP 服务器的安装与设定前言 2. 所需软件与软件结构 3. 主要配置文件 ntp.conf 的处理 4. NTP ...
- QQ发送位置(高德地图)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- 分布式_事务_02_2PC框架raincat源码解析
一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat的源码 二.协调者启动过程 主要就是在启动类中通过如下代码来启动 netty nettyService.sta ...