Java-反射机制(超详解)

news2024/11/18 4:43:34

Java反射机制概述

  • 前言
  • 一、Java反射机制概述
  • 1. Java Reflection
  • 2. 动态语言 vs 静态语言
  • 二、 Class类的理解
  • 1. 类的加载过程
  • 1.1 初步了解
  • 1.2 类的加载过程图解
  • 1.3 了解:什么时候会发生类初始化?
  • 1.4 类加载器的作用
  • 1.5 JVM中不同类型的类的加载器
  • 1.6 代码演示
  • 2. 何为Class类?
  • 3. Class类的常用方法方法
  • 3. 哪些类型可以有Class对象?
  • 三、获取Class类实例的四种方法
  • 1. 调用运行时类的属性:.class
  • 2. 通过运行时类的对象,调用getClass()
  • 3.调用Class的静态方法:forName(String classPath)
  • 4. 使用类的加载器:ClassLoader
  • 5. 代码演示
  • 四、 创建运行时类的对象
  • 1. 引入
  • 2. 语法步骤
  • 3. 代码演示
  • 4. 体会反射的动态性
  • 五、获取运行时类的完整结构
  • 1. 获取当前运行时类的属性结构
  • 2. 获取当前运行时类的方法结构
  • 3. 获取当前运行时类的构造器结构
  • 六、调用运行时类的指定结构
  • 1. 调用运行时类中指定的属性
  • 2. 调用运行时类中的指定的方法
  • 3. 调用运行时类中的指定的构造器



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!
也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!让我们在成长的道路上互相学习,欢迎关注!

一、Java反射机制概述

1. Java Reflection

(1)Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期 借助于ReflectionAPI取得任何类的内部信息,并能直接操作任意对象的内 部属性及方法。

(2)加载完类之后,在堆内存的方法区中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,我们形象的称之为:反射。
在这里插入图片描述

2. 动态语言 vs 静态语言

(1)动态语言

是一类在运行时可以改变其结构的语言:例如新的函数、对象、甚至代码可以 被引进,已有的函数可以被删除或是其他结构上的变化。通俗点说就是在运行时代码可以根据某些条件改变自身结构。

主要动态语言:Object-C、C#、JavaScript、PHP、Python、Erlang。

(2)静态语言

与动态语言相对应的,运行时结构不可变的语言就是静态语言。如Java、C、C++。Java不是动态语言,但Java可以称之为“准动态语言”。即Java有一定的动态性,我们可以利用反射机制、字节码操作获得类似动态语言的特性。 Java的动态性让编程的时候更加灵活!

(3)Java反射机制研究及应用

? Java反射机制提供的功能

  1. 在运行时判断任意一个对象所属的类
  2. 在运行时构造任意一个类的对象
  3. 在运行时判断任意一个类所具有的成员变量和方法
  4. 在运行时获取泛型信息 在运行时调用任意一个对象的成员变量和方法
  5. 在运行时处理注解 生成动态代理

? 反射相关的主要API

  1. java.lang.Class:代表一个类
  2. java.lang.reflect.Method:代表类的方法
  3. java.lang.reflect.Field:代表类的成员变量
  4. java.lang.reflect.Constructor:代表类的构造器 ? … …

二、 Class类的理解

1. 类的加载过程

1.1 初步了解

? 程序经过javac.exe命令以后,会生成一个或多个字节码文件(.class结尾)。
接着我们使用java.exe命令对某个字节码文件进行解释运行。相当于将某个字节码文件加载到内存中。此过程就称为类的加载。加载到内存中的类,我们就称为运行时类,此运行时类,就作为Class的一个实例。

? 换句话说,Class的实例就对应着一个运行时类。

? 加载到内存中的运行时类,会缓存一定的时间。在此时间之内,我们可以通过不同的方式来获取此运行时类。

1.2 类的加载过程图解

当程序主动使用某个类时,如果该类还未被加载到内存中,则系统会通过如下三个步骤来对该类进行初始化。

在这里插入图片描述

? 类的加载:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后生成一个代表这个类的java.lang.Class对象,作为方法区中类数据的访问入口(即引用地址)。所有需要访问和使用类数据只能通过这个Class对象。这个加载的过程需要类加载器参与。

? 类的链接:将Java类的二进制代码合并到JVM的运行状态之中的过程。
● 验证:确保加载的类信息符合JVM规范,例如:以cafe开头,没有安全方面的问题
● 准备:正式为类变量(static)分配内存并设置类变量默认初始值的阶段,这些内存 都将在方法区中进行分配。
● 解析:虚拟机常量池内的符号引用(常量名)替换为直接引用(地址)的过程。

? 类的初始化:
● 执行类构造器【clinit】()方法的过程。类构造器【clinit】()方法是由编译期自动收集类中 所有类变量的赋值动作和静态代码块中的语句合并产生的。(类构造器是构造类信 息的,不是构造该类对象的构造器)。
● 当初始化一个类的时候,如果发现其父类还没有进行初始化,则需要先触发其父类 的初始化。
● 虚拟机会保证一个类的()方法在多线程环境中被正确加锁和同步。

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#ff79c6">public</span> <span style="color:#ff79c6">class</span> ClassLoadingTest <span style="color:#999999">{</span>
<span style="color:#ff79c6">public</span> <span style="color:#ff79c6">static</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>A<span style="color:#999999">.</span>m<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#999999">}</span>
<span style="color:#ff79c6">class</span> A <span style="color:#999999">{</span>
<span style="color:#ff79c6">static</span> <span style="color:#999999">{</span> m <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">300</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#ff79c6">static</span> <span style="color:#ff79c6">int</span> m <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">100</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#6272a4">//第二步:链接结束后m=0</span>
<span style="color:#6272a4">//第三步:初始化后,m的值由<clinit>()方法执行决定</span>
<span style="color:#6272a4">// 这个A的类构造器<clinit>()方法由类变量的赋值和静态代码块中的语句按照顺序合并产生,类似于</span>
<span style="color:#6272a4">// <clinit>(){</span>
<span style="color:#6272a4">// m = 300;</span>
<span style="color:#6272a4">// m = 100;</span>
<span style="color:#6272a4">// }</span>
</code></span></span></span>

1.3 了解:什么时候会发生类初始化?

? 类的主动引用(一定会发生类的初始化)

  1. 当虚拟机启动,先初始化main方法所在的类
  2. new一个类的对象
  3. 调用类的静态成员(除了final常量)和静态方法
  4. 使用java.lang.reflect包的方法对类进行反射调用
  5. 当初始化一个类,如果其父类没有被初始化,则先会初始化它的父类

? 类的被动引用(不会发生类的初始化)

  1. 当访问一个静态域时,只有真正声明这个域的类才会被初始化
  2. 当通过子类引用父类的静态变量,不会导致子类初始化
  3. 通过数组定义类引用,不会触发此类的初始化
  4. 引用常量不会触发此类的初始化(常量在链接阶段就存入调用类的常量池中了)

1.4 类加载器的作用

? 类加载的作用:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的java.lang.Class对象,作为 方法区中类数据的访问入口。
? 类缓存:标准的JavaSE类加载器可以按要求查找类,但一旦某个类被加载到类加载器 中,它将维持加载(缓存)一段时间。不过JVM垃圾回收机制可以回收这些Class对象。

在这里插入图片描述

1.5 JVM中不同类型的类的加载器

在这里插入图片描述

1.6 代码演示

? 不同类型的类的加载器:

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"> <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#6272a4">//对于自定义类,使用系统类加载器进行加载</span>
        ClassLoader classLoader <span style="color:#6272a4">=</span> ClassLoaderTest<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">.</span><span style="color:#8be9fd">getClassLoader</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>classLoader<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//sun.misc.Launcher$AppClassLoader@18b4aac2:系统类加载器</span>
        <span style="color:#6272a4">//调用系统类加载器的getParent():获取扩展类加载器</span>
        ClassLoader classLoader1 <span style="color:#6272a4">=</span> classLoader<span style="color:#999999">.</span><span style="color:#8be9fd">getParent</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>classLoader1<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//sun.misc.Launcher$ExtClassLoader@279f2327:扩展类加载器</span>
        <span style="color:#6272a4">//调用扩展类加载器的getParent():无法获取引导类加载器</span>
        <span style="color:#6272a4">//引导类加载器主要负责加载java的核心类库,无法加载自定义类的。</span>
        ClassLoader classLoader2 <span style="color:#6272a4">=</span> classLoader1<span style="color:#999999">.</span><span style="color:#8be9fd">getParent</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>classLoader2<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//null</span>

        ClassLoader classLoader3 <span style="color:#6272a4">=</span> String<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">.</span><span style="color:#8be9fd">getClassLoader</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>classLoader3<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//null</span>

    <span style="color:#999999">}</span>
</code></span></span></span>

? 使用系统类加载器读取Properties配置文件。

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"> <span style="color:#6272a4">/*
    Properties:用来读取配置文件。

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test2</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>

        Properties pros <span style="color:#6272a4">=</span>  <span style="color:#ff79c6">new</span> Properties<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//此时的文件默认在当前的module下。</span>
        <span style="color:#6272a4">//读取配置文件的方式一:</span>
<span style="color:#6272a4">//        FileInputStream fis = new FileInputStream("jdbc.properties");</span>
<span style="color:#6272a4">//        FileInputStream fis = new FileInputStream("src\\jdbc1.properties");</span>
<span style="color:#6272a4">//        pros.load(fis);</span>

        <span style="color:#6272a4">//读取配置文件的方式二:使用ClassLoader</span>
        <span style="color:#6272a4">//配置文件默认识别为:当前module的src下</span>
        ClassLoader classLoader <span style="color:#6272a4">=</span> ClassLoaderTest<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">.</span><span style="color:#8be9fd">getClassLoader</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        InputStream is <span style="color:#6272a4">=</span> classLoader<span style="color:#999999">.</span><span style="color:#8be9fd">getResourceAsStream</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"jdbc1.properties"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        pros<span style="color:#999999">.</span><span style="color:#8be9fd">load</span><span style="color:#999999">(</span>is<span style="color:#999999">)</span><span style="color:#999999">;</span>


        String user <span style="color:#6272a4">=</span> pros<span style="color:#999999">.</span><span style="color:#8be9fd">getProperty</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"user"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        String password <span style="color:#6272a4">=</span> pros<span style="color:#999999">.</span><span style="color:#8be9fd">getProperty</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"password"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"user = "</span> <span style="color:#6272a4">+</span> user <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">",password = "</span> <span style="color:#6272a4">+</span> password<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span>

2. 何为Class类?

Class类在Object类中定义了以下的方法,此方法将被所有子类继承:

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#ff79c6">public</span> <span style="color:#ff79c6">final</span> Class <span style="color:#8be9fd">getClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
</code></span></span></span>

以上的方法返回值的类型是一个Class类,此类是Java反射的源头,实际上所谓反射从程序的运行结果来看也很好理解,即:可以通过对象反射求出类的名称。

在这里插入图片描述

? 对象照镜子后可以得到的信息:某个类的属性、方法和构造器、某个类到底实现了哪些接口。对于每个类而言,JRE 都为其保留一个不变的Class类型的对象。
一个 Class对象包含了特定某个结构(class/interface/enum/annotation/primitivetype/void/[])的有关信息。

Class本身也是一个类

Class 对象只能由系统建立对象

? 一个加载的类在 JVM中只会有一个Class实例

? 一个Class对象对应的是一个加载到JVM中的一个.class文件

? 每个类的实例都会记得自己是由哪个Class 实例所生成

? 通过Class可以完整地得到一个类中的所有被加载的结构

Class类是Reflection的根源,针对任何你想动态加载、运行的类,唯有先获得相应的

3. Class类的常用方法方法

方法名功能说明
static Class forName(String name)返回指定类名 name 的 Class 对象
Object newInstance()调用缺省构造函数,返回该Class对象的一个实例
getName()返回此Class对象所表示的实体(类、接口、数组类、基本类型或void)名称
Class getSuperClass()返回当前Class对象的父类的Class对象
Class [] getInterfaces()获取当前Class对象的接口
ClassLoader getClassLoader()返回该类的类加载器
Class getSuperclass()返回表示此Class所表示的实体的超类的Class
Constructor[] getConstructors()返回一个包含某些Constructor对象的数组
Field[] getDeclaredFields()返回Field对象的一个数组
Method getMethod(String name,Class … paramTypes)返回一个Method对象,此对象的形参类型为paramType

3. 哪些类型可以有Class对象?

(1)class: 外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类
(2)interface:接口
(3)[]:数组
(4)enum:枚举
(5)annotation:注解@interface
(6)primitive type:基本数据类型
(7)void

三、获取Class类实例的四种方法

1. 调用运行时类的属性:.class

前提:若已知具体的类,通过类的class属性获取,该方法最为安全可靠, 程序性能最高
示例: Class clazz1 = String.class;

2. 通过运行时类的对象,调用getClass()

前提:已知某个类的实例,调用该实例的getClass()方法获取Class对象
示例:Class clazz = “www.atguigu.com”.getClass();

3.调用Class的静态方法:forName(String classPath)

前提:已知一个类的全类名,且该类在类路径下,可通过Class类的静态方法forName() 获取,可能抛出ClassNotFoundException
示例: Class clazz = Class.forName(“java.lang.String”);

4. 使用类的加载器:ClassLoader

示例:
ClassLoader cl = this.getClass().getClassLoader();
Class clazz4 = cl.loadClass(“类的全类名”);

5. 代码演示

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#999999">@Test</span>
<span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> ClassNotFoundException <span style="color:#999999">{</span>
            <span style="color:#6272a4">//方式一:调用运行时类的属性:.class</span>
            Class clazz1 <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz1<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//class com.jiaying.java1.Person</span>
            <span style="color:#6272a4">//方式二:通过运行时类的对象,调用getClass()</span>
            Person p1 <span style="color:#6272a4">=</span> <span style="color:#ff79c6">new</span> Person<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            Class clazz2 <span style="color:#6272a4">=</span> p1<span style="color:#999999">.</span><span style="color:#8be9fd">getClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz2<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//class com.jiaying.java1.Person</span>

            <span style="color:#6272a4">//方式三:调用Class的静态方法:forName(String classPath)</span>
            Class clazz3 <span style="color:#6272a4">=</span> Class<span style="color:#999999">.</span><span style="color:#8be9fd">forName</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"com.jiaying.java1.Person"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            Class clazz5 <span style="color:#6272a4">=</span> Class<span style="color:#999999">.</span><span style="color:#8be9fd">forName</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"java.lang.String"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz3<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//class com.jiaying.java1.Person</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz5<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//class java.lang.String</span>

            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz1 <span style="color:#6272a4">==</span> clazz2<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//true</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz1 <span style="color:#6272a4">==</span> clazz3<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//true</span>

            <span style="color:#6272a4">//方式四:使用类的加载器:ClassLoader  (了解)</span>
            ClassLoader classLoader <span style="color:#6272a4">=</span> ReflectionTest<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">.</span><span style="color:#8be9fd">getClassLoader</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            Class clazz4 <span style="color:#6272a4">=</span> classLoader<span style="color:#999999">.</span><span style="color:#8be9fd">loadClass</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"com.jiaying.java1.Person"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz4<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//class com.jiaying.java1.Person</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>clazz1 <span style="color:#6272a4">==</span> clazz4<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//true</span>
<span style="color:#999999">}</span>
</code></span></span></span>

四、 创建运行时类的对象

1. 引入

? 有了Class对象,能做什么?

创建类的对象:调用Class对象的newInstance()方法
要求:

  1. 类必须有一个无参数的构造器。
  2. 类的构造器的访问权限需要足够。

? 难道没有无参的构造器就不能创建对象了吗?
不是!只要在操作的时候明确的调用类中的构造器,并将参数传递进去之后,才可以实例化操作。
步骤如下:

  1. 通过Class类的getDeclaredConstructor(Class … parameterTypes)取得本类的指定形参类型的构造器
  2. 向构造器的形参中传递一个对象数组进去,里面包含了构造器中所需的各个参数。
  3. 通过Constructor实例化对象。

2. 语法步骤

(1)根据全类名获取对应的Class对象

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">String name <span style="color:#6272a4">=</span> “atguigu<span style="color:#999999">.</span>java<span style="color:#999999">.</span>Person"<span style="color:#999999">;</span>
Class clazz <span style="color:#6272a4">=</span> <span style="color:#ff79c6">null</span><span style="color:#999999">;</span>
clazz <span style="color:#6272a4">=</span> Class<span style="color:#999999">.</span><span style="color:#8be9fd">forName</span><span style="color:#999999">(</span>name<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span></span></span>

(2)调用指定参数结构的构造器,生成Constructor的实例

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">Constructor con <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getConstructor</span><span style="color:#999999">(</span>String<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">,</span>Integer<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span></span></span>

(3)通过Constructor的实例创建对应类的对象,并初始化类属性

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">Person p2 <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>Person<span style="color:#999999">)</span> con<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"Peter"</span><span style="color:#999999">,</span><span style="color:#f1fa8c">20</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>p2<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span></span></span>

3. 代码演示

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"> <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> IllegalAccessException<span style="color:#999999">,</span> InstantiationException <span style="color:#999999">{</span>

        Class<span style="color:#999999"><</span>Person<span style="color:#999999">></span> clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">/*
        newInstance():调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。

        要想此方法正常的创建运行时类的对象,要求:
        1.运行时类必须提供空参的构造器
        2.空参的构造器的访问权限得够。通常,设置为public。


        在javabean中要求提供一个public的空参构造器。原因:
        1.便于通过反射,创建运行时类的对象
        2.便于子类继承此运行时类时,默认调用super()时,保证父类有此构造器

         */</span>
        Person obj <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>obj<span style="color:#999999">)</span><span style="color:#999999">;</span>

    <span style="color:#999999">}</span>

</code></span></span></span>

4. 体会反射的动态性

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#6272a4">//体会反射的动态性</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test2</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>

        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span><span style="color:#ff79c6">int</span> i <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">0</span><span style="color:#999999">;</span>i <span style="color:#6272a4"><</span> <span style="color:#f1fa8c">100</span><span style="color:#999999">;</span>i<span style="color:#6272a4">++</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#ff79c6">int</span> num <span style="color:#6272a4">=</span> <span style="color:#ff79c6">new</span> Random<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#8be9fd">nextInt</span><span style="color:#999999">(</span><span style="color:#f1fa8c">3</span><span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//0,1,2</span>
            String classPath <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">""</span><span style="color:#999999">;</span>
            <span style="color:#ff79c6">switch</span><span style="color:#999999">(</span>num<span style="color:#999999">)</span><span style="color:#999999">{</span>
                <span style="color:#ff79c6">case</span> <span style="color:#f1fa8c">0</span><span style="color:#6272a4">:</span>
                    classPath <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">"java.util.Date"</span><span style="color:#999999">;</span>
                    <span style="color:#ff79c6">break</span><span style="color:#999999">;</span>
                <span style="color:#ff79c6">case</span> <span style="color:#f1fa8c">1</span><span style="color:#6272a4">:</span>
                    classPath <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">"java.lang.Object"</span><span style="color:#999999">;</span>
                    <span style="color:#ff79c6">break</span><span style="color:#999999">;</span>
                <span style="color:#ff79c6">case</span> <span style="color:#f1fa8c">2</span><span style="color:#6272a4">:</span>
                    classPath <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">"com.atguigu.java.Person"</span><span style="color:#999999">;</span>
                    <span style="color:#ff79c6">break</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>

            <span style="color:#ff79c6">try</span> <span style="color:#999999">{</span>
                Object obj <span style="color:#6272a4">=</span> <span style="color:#8be9fd">getInstance</span><span style="color:#999999">(</span>classPath<span style="color:#999999">)</span><span style="color:#999999">;</span>
                System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>obj<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span> <span style="color:#ff79c6">catch</span> <span style="color:#999999">(</span>Exception e<span style="color:#999999">)</span> <span style="color:#999999">{</span>
                e<span style="color:#999999">.</span><span style="color:#8be9fd">printStackTrace</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>

    <span style="color:#6272a4">/*
    创建一个指定类的对象。
    classPath:指定类的全类名
     */</span>
    <span style="color:#ff79c6">public</span> Object <span style="color:#8be9fd">getInstance</span><span style="color:#999999">(</span>String classPath<span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>
       Class clazz <span style="color:#6272a4">=</span>  Class<span style="color:#999999">.</span><span style="color:#8be9fd">forName</span><span style="color:#999999">(</span>classPath<span style="color:#999999">)</span><span style="color:#999999">;</span>
       <span style="color:#ff79c6">return</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span>

五、获取运行时类的完整结构

提供具有丰富内容的Person

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#6272a4">//接口</span>
<span style="color:#ff79c6">public</span> <span style="color:#ff79c6">interface</span> MyInterface <span style="color:#999999">{</span>
    <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">info</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>

<span style="color:#6272a4">//注解</span>
<span style="color:#999999">@Target</span><span style="color:#999999">(</span><span style="color:#999999">{</span>TYPE<span style="color:#999999">,</span> FIELD<span style="color:#999999">,</span> METHOD<span style="color:#999999">,</span> PARAMETER<span style="color:#999999">,</span> CONSTRUCTOR<span style="color:#999999">,</span> LOCAL_VARIABLE<span style="color:#999999">}</span><span style="color:#999999">)</span>
<span style="color:#999999">@Retention</span><span style="color:#999999">(</span>RetentionPolicy<span style="color:#999999">.</span>RUNTIME<span style="color:#999999">)</span>
<span style="color:#ff79c6">public</span> <span style="color:#999999">@interface</span> MyAnnotation <span style="color:#999999">{</span>
    String <span style="color:#8be9fd">value</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">default</span> <span style="color:#f1fa8c">"hello"</span><span style="color:#999999">;</span>

<span style="color:#999999">}</span>

<span style="color:#6272a4">//父类</span>
<span style="color:#ff79c6">public</span> <span style="color:#ff79c6">class</span> Creature<span style="color:#999999"><</span>T<span style="color:#999999">></span> <span style="color:#ff79c6">implements</span> Serializable <span style="color:#999999">{</span>
    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">char</span> gender<span style="color:#999999">;</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">double</span> weight<span style="color:#999999">;</span>

    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">breath</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"生物呼吸"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">eat</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"生物吃东西"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

<span style="color:#999999">}</span>

<span style="color:#6272a4">//Person类</span>
<span style="color:#999999">@MyAnnotation</span><span style="color:#999999">(</span>value<span style="color:#6272a4">=</span><span style="color:#f1fa8c">"hi"</span><span style="color:#999999">)</span>
<span style="color:#ff79c6">public</span> <span style="color:#ff79c6">class</span> Person <span style="color:#ff79c6">extends</span> Creature<span style="color:#999999"><</span>String<span style="color:#999999">></span> <span style="color:#ff79c6">implements</span> Comparable<span style="color:#999999"><</span>String<span style="color:#999999">></span><span style="color:#999999">,</span>MyInterface<span style="color:#999999">{</span>

    <span style="color:#ff79c6">private</span> String name<span style="color:#999999">;</span>
    <span style="color:#ff79c6">int</span> age<span style="color:#999999">;</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">int</span> id<span style="color:#999999">;</span>

    <span style="color:#ff79c6">public</span> Person<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span><span style="color:#999999">}</span>

    <span style="color:#999999">@MyAnnotation</span><span style="color:#999999">(</span>value<span style="color:#6272a4">=</span><span style="color:#f1fa8c">"abc"</span><span style="color:#999999">)</span>
    <span style="color:#ff79c6">private</span> Person<span style="color:#999999">(</span>String name<span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#ff79c6">this</span><span style="color:#999999">.</span>name <span style="color:#6272a4">=</span> name<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

     Person<span style="color:#999999">(</span>String name<span style="color:#999999">,</span><span style="color:#ff79c6">int</span> age<span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#ff79c6">this</span><span style="color:#999999">.</span>name <span style="color:#6272a4">=</span> name<span style="color:#999999">;</span>
        <span style="color:#ff79c6">this</span><span style="color:#999999">.</span>age <span style="color:#6272a4">=</span> age<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
    <span style="color:#999999">@MyAnnotation</span>
    <span style="color:#ff79c6">private</span> String <span style="color:#8be9fd">show</span><span style="color:#999999">(</span>String nation<span style="color:#999999">)</span><span style="color:#999999">{</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"我的国籍是:"</span> <span style="color:#6272a4">+</span> nation<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">return</span> nation<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#ff79c6">public</span> String <span style="color:#8be9fd">display</span><span style="color:#999999">(</span>String interests<span style="color:#999999">,</span><span style="color:#ff79c6">int</span> age<span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> NullPointerException<span style="color:#999999">,</span>ClassCastException<span style="color:#999999">{</span>
        <span style="color:#ff79c6">return</span> interests <span style="color:#6272a4">+</span> age<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>


    <span style="color:#999999">@Override</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">info</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"我是一个人"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#999999">@Override</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">int</span> <span style="color:#8be9fd">compareTo</span><span style="color:#999999">(</span>String o<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        <span style="color:#ff79c6">return</span> <span style="color:#f1fa8c">0</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">static</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">showDesc</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"我是一个可爱的人"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#999999">@Override</span>
    <span style="color:#ff79c6">public</span> String <span style="color:#8be9fd">toString</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
        <span style="color:#ff79c6">return</span> <span style="color:#f1fa8c">"Person{"</span> <span style="color:#6272a4">+</span>
                <span style="color:#f1fa8c">"name='"</span> <span style="color:#6272a4">+</span> name <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">'\''</span> <span style="color:#6272a4">+</span>
                <span style="color:#f1fa8c">", age="</span> <span style="color:#6272a4">+</span> age <span style="color:#6272a4">+</span>
                <span style="color:#f1fa8c">", id="</span> <span style="color:#6272a4">+</span> id <span style="color:#6272a4">+</span>
                <span style="color:#f1fa8c">'}'</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span>

1. 获取当前运行时类的属性结构

方法作用
public Field[] getFields()返回此Class对象所表示的类或接口的publicField
public Field[] getDeclaredFields()返回此Class对象所表示的类或接口的全部Field
  • Field方法中:
方法作用
public int getModifiers()以整数形式返回此Field的修饰符
public Class<?> getType()得到Field的属性类型
public String getName()返回Field的名称
<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>

        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//获取属性结构</span>
        <span style="color:#6272a4">//getFields():获取当前运行时类及其父类中声明为public访问权限的属性</span>
        Field<span style="color:#999999">[</span><span style="color:#999999">]</span> fields <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getFields</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Field f <span style="color:#6272a4">:</span> fields<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>f<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//getDeclaredFields():获取当前运行时类中声明的所有属性。(不包含父类中声明的属性)</span>
        Field<span style="color:#999999">[</span><span style="color:#999999">]</span> declaredFields <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredFields</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Field f <span style="color:#6272a4">:</span> declaredFields<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>f<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>

    <span style="color:#6272a4">//权限修饰符  数据类型 变量名</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test2</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>
        Field<span style="color:#999999">[</span><span style="color:#999999">]</span> declaredFields <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredFields</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Field f <span style="color:#6272a4">:</span> declaredFields<span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#6272a4">//1.权限修饰符</span>
            <span style="color:#ff79c6">int</span> modifier <span style="color:#6272a4">=</span> f<span style="color:#999999">.</span><span style="color:#8be9fd">getModifiers</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>Modifier<span style="color:#999999">.</span><span style="color:#8be9fd">toString</span><span style="color:#999999">(</span>modifier<span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">"\t"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

            <span style="color:#6272a4">//2.数据类型</span>
            Class type <span style="color:#6272a4">=</span> f<span style="color:#999999">.</span><span style="color:#8be9fd">getType</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>type<span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">"\t"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

            <span style="color:#6272a4">//3.变量名</span>
            String fName <span style="color:#6272a4">=</span> f<span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>fName<span style="color:#999999">)</span><span style="color:#999999">;</span>

            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>

</code></span></span></span>

2. 获取当前运行时类的方法结构

方法作用
public Method[] getMethods()返回此Class对象所表示的类或接口的public的方法
public Method[] getDeclaredMethods()返回此Class对象所表示的类或接口的全部方法
  • Method类中:
方法作用
public Class<?> getReturnType()取得全部的返回值
public Class<?>[] getParameterTypes()取得全部的参数
public int getModifiers()取得修饰符
public Class<?>[] getExceptionTypes()取得异常信息
<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>

        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//getMethods():获取当前运行时类及其所有父类中声明为public权限的方法</span>
        Method<span style="color:#999999">[</span><span style="color:#999999">]</span> methods <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getMethods</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Method m <span style="color:#6272a4">:</span> methods<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>m<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)</span>
        Method<span style="color:#999999">[</span><span style="color:#999999">]</span> declaredMethods <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredMethods</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Method m <span style="color:#6272a4">:</span> declaredMethods<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>m<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
  <span style="color:#6272a4">/*
    @Xxxx
    权限修饰符  返回值类型  方法名(参数类型1 形参名1,...) throws XxxException{}
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test2</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>
        Method<span style="color:#999999">[</span><span style="color:#999999">]</span> declaredMethods <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredMethods</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Method m <span style="color:#6272a4">:</span> declaredMethods<span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#6272a4">//1.获取方法声明的注解</span>
            Annotation<span style="color:#999999">[</span><span style="color:#999999">]</span> annos <span style="color:#6272a4">=</span> m<span style="color:#999999">.</span><span style="color:#8be9fd">getAnnotations</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Annotation a <span style="color:#6272a4">:</span> annos<span style="color:#999999">)</span><span style="color:#999999">{</span>
                System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>a<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>

            <span style="color:#6272a4">//2.权限修饰符</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>Modifier<span style="color:#999999">.</span><span style="color:#8be9fd">toString</span><span style="color:#999999">(</span>m<span style="color:#999999">.</span><span style="color:#8be9fd">getModifiers</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">"\t"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

            <span style="color:#6272a4">//3.返回值类型</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>m<span style="color:#999999">.</span><span style="color:#8be9fd">getReturnType</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">"\t"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

            <span style="color:#6272a4">//4.方法名</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>m<span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"("</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#6272a4">//5.形参列表</span>
            Class<span style="color:#999999">[</span><span style="color:#999999">]</span> parameterTypes <span style="color:#6272a4">=</span> m<span style="color:#999999">.</span><span style="color:#8be9fd">getParameterTypes</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#ff79c6">if</span><span style="color:#999999">(</span><span style="color:#6272a4">!</span><span style="color:#999999">(</span>parameterTypes <span style="color:#6272a4">==</span> <span style="color:#ff79c6">null</span> <span style="color:#6272a4">&&</span> parameterTypes<span style="color:#999999">.</span>length <span style="color:#6272a4">==</span> <span style="color:#f1fa8c">0</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                <span style="color:#ff79c6">for</span><span style="color:#999999">(</span><span style="color:#ff79c6">int</span> i <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">0</span><span style="color:#999999">;</span>i <span style="color:#6272a4"><</span> parameterTypes<span style="color:#999999">.</span>length<span style="color:#999999">;</span>i<span style="color:#6272a4">++</span><span style="color:#999999">)</span><span style="color:#999999">{</span>

                    <span style="color:#ff79c6">if</span><span style="color:#999999">(</span>i <span style="color:#6272a4">==</span> parameterTypes<span style="color:#999999">.</span>length <span style="color:#6272a4">-</span> <span style="color:#f1fa8c">1</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>parameterTypes<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">" args_"</span> <span style="color:#6272a4">+</span> i<span style="color:#999999">)</span><span style="color:#999999">;</span>
                        <span style="color:#ff79c6">break</span><span style="color:#999999">;</span>
                    <span style="color:#999999">}</span>

                    System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>parameterTypes<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">" args_"</span> <span style="color:#6272a4">+</span> i <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">","</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
                <span style="color:#999999">}</span>
            <span style="color:#999999">}</span>

            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span><span style="color:#f1fa8c">")"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

            <span style="color:#6272a4">//6.抛出的异常</span>
            Class<span style="color:#999999">[</span><span style="color:#999999">]</span> exceptionTypes <span style="color:#6272a4">=</span> m<span style="color:#999999">.</span><span style="color:#8be9fd">getExceptionTypes</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#ff79c6">if</span><span style="color:#999999">(</span>exceptionTypes<span style="color:#999999">.</span>length <span style="color:#6272a4">></span> <span style="color:#f1fa8c">0</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"throws "</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
                <span style="color:#ff79c6">for</span><span style="color:#999999">(</span><span style="color:#ff79c6">int</span> i <span style="color:#6272a4">=</span> <span style="color:#f1fa8c">0</span><span style="color:#999999">;</span>i <span style="color:#6272a4"><</span> exceptionTypes<span style="color:#999999">.</span>length<span style="color:#999999">;</span>i<span style="color:#6272a4">++</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                    <span style="color:#ff79c6">if</span><span style="color:#999999">(</span>i <span style="color:#6272a4">==</span> exceptionTypes<span style="color:#999999">.</span>length <span style="color:#6272a4">-</span> <span style="color:#f1fa8c">1</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>exceptionTypes<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
                        <span style="color:#ff79c6">break</span><span style="color:#999999">;</span>
                    <span style="color:#999999">}</span>

                    System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">print</span><span style="color:#999999">(</span>exceptionTypes<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#6272a4">+</span> <span style="color:#f1fa8c">","</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
                <span style="color:#999999">}</span>
            <span style="color:#999999">}</span>


            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>



    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>

</code></span></span></span>

3. 获取当前运行时类的构造器结构

方法作用
public Constructor<T>[] getConstructors()返回此 Class 对象所表示的类的所有public构造方法。
public Constructor<T>[] getDeclaredConstructors()返回此 Class 对象表示的类声明的所有构造方法。
  • Constructor类中:
方法作用
public int getModifiers()取得修饰符
public String getName()取得方法名称
public Class<?>[] getParameterTypes()取得参数的类型
<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#6272a4">/*
    获取构造器结构

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test1</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>

        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//getConstructors():获取当前运行时类中声明为public的构造器</span>
        Constructor<span style="color:#999999">[</span><span style="color:#999999">]</span> constructors <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getConstructors</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Constructor c <span style="color:#6272a4">:</span> constructors<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>c<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>

        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//getDeclaredConstructors():获取当前运行时类中声明的所有的构造器</span>
        Constructor<span style="color:#999999">[</span><span style="color:#999999">]</span> declaredConstructors <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredConstructors</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Constructor c <span style="color:#6272a4">:</span> declaredConstructors<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>c<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>

    <span style="color:#999999">}</span>
 <span style="color:#6272a4">/*
    获取运行时类的父类

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test2</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Class superclass <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getSuperclass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>superclass<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#6272a4">/*
    获取运行时类的带泛型的父类

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test3</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Type genericSuperclass <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getGenericSuperclass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>genericSuperclass<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#6272a4">/*
    获取运行时类的带泛型的父类的泛型


    代码:逻辑性代码  vs 功能性代码
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test4</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Type genericSuperclass <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getGenericSuperclass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        ParameterizedType paramType <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>ParameterizedType<span style="color:#999999">)</span> genericSuperclass<span style="color:#999999">;</span>
        <span style="color:#6272a4">//获取泛型类型</span>
        Type<span style="color:#999999">[</span><span style="color:#999999">]</span> actualTypeArguments <span style="color:#6272a4">=</span> paramType<span style="color:#999999">.</span><span style="color:#8be9fd">getActualTypeArguments</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6272a4">//        System.out.println(actualTypeArguments[0].getTypeName());</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">(</span><span style="color:#999999">(</span>Class<span style="color:#999999">)</span>actualTypeArguments<span style="color:#999999">[</span><span style="color:#f1fa8c">0</span><span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#8be9fd">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#6272a4">/*
    获取运行时类实现的接口
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test5</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Class<span style="color:#999999">[</span><span style="color:#999999">]</span> interfaces <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getInterfaces</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Class c <span style="color:#6272a4">:</span> interfaces<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>c<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>

        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//获取运行时类的父类实现的接口</span>
        Class<span style="color:#999999">[</span><span style="color:#999999">]</span> interfaces1 <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getSuperclass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#8be9fd">getInterfaces</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Class c <span style="color:#6272a4">:</span> interfaces1<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>c<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>

    <span style="color:#999999">}</span>
    <span style="color:#6272a4">/*
        获取运行时类所在的包

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test6</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Package pack <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getPackage</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>pack<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#6272a4">/*
        获取运行时类声明的注解

     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">test7</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        Annotation<span style="color:#999999">[</span><span style="color:#999999">]</span> annotations <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getAnnotations</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#ff79c6">for</span><span style="color:#999999">(</span>Annotation annos <span style="color:#6272a4">:</span> annotations<span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>annos<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>

<span style="color:#999999">}</span>

</code></span></span></span>

六、调用运行时类的指定结构

关于setAccessible方法的使用

MethodFieldConstructor对象都有setAccessible()方法。

setAccessible启动和禁用访问安全检查的开关。

? 参数值为true则指示反射的对象在使用时应该取消Java语言访问检查。

? 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被 调用,那么请设置为true,使得原本无法访问的私有成员也可以访问,参数值为false则指示反射的对象应该实施Java语言访问检查。

1. 调用运行时类中指定的属性

在反射机制中,可以直接通过Field类操作类中的属性,通过Field类提供的set()和 get()方法就可以完成设置和取得属性内容的操作。

方法作用
public Field getField(String name)返回此Class对象表示的类或接口的指定的publicField
public Field getDeclaredField(String name)返回此Class对象表示的类或接口的指定的Field

在Field中:

方法作用
public Object get(Object obj)取得指定对象obj上此Field的属性内容
public void set(Object obj,Object value)设置指定对象obj上此Field的属性内容

代码演示:

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"><span style="color:#ff79c6">public</span> <span style="color:#ff79c6">class</span> ReflectionTest <span style="color:#999999">{</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">testField</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//创建运行时类的对象</span>
        Person p <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>Person<span style="color:#999999">)</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>


        <span style="color:#6272a4">//获取指定的属性:要求运行时类中属性声明为public</span>
        <span style="color:#6272a4">//通常不采用此方法</span>
        Field id <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getField</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"id"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">/*
        设置当前属性的值

        set():参数1:指明设置哪个对象的属性   参数2:将此属性值设置为多少
         */</span>

        id<span style="color:#999999">.</span><span style="color:#8be9fd">set</span><span style="color:#999999">(</span>p<span style="color:#999999">,</span><span style="color:#f1fa8c">1001</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">/*
        获取当前属性的值
        get():参数1:获取哪个对象的当前属性值
         */</span>
        <span style="color:#ff79c6">int</span> pId <span style="color:#6272a4">=</span> <span style="color:#999999">(</span><span style="color:#ff79c6">int</span><span style="color:#999999">)</span> id<span style="color:#999999">.</span><span style="color:#8be9fd">get</span><span style="color:#999999">(</span>p<span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>pId<span style="color:#999999">)</span><span style="color:#999999">;</span>


    <span style="color:#999999">}</span>
    <span style="color:#6272a4">/*
    如何操作运行时类中的指定的属性 -- 需要掌握
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">testField1</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//创建运行时类的对象</span>
        Person p <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>Person<span style="color:#999999">)</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//1. getDeclaredField(String fieldName):获取运行时类中指定变量名的属性</span>
        Field name <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredField</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"name"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//2.保证当前属性是可访问的</span>
        name<span style="color:#999999">.</span><span style="color:#8be9fd">setAccessible</span><span style="color:#999999">(</span><span style="color:#8be9fd">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//3.获取、设置指定对象的此属性值</span>
        name<span style="color:#999999">.</span><span style="color:#8be9fd">set</span><span style="color:#999999">(</span>p<span style="color:#999999">,</span><span style="color:#f1fa8c">"Tom"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>name<span style="color:#999999">.</span><span style="color:#8be9fd">get</span><span style="color:#999999">(</span>p<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

</code></span></span></span>

2. 调用运行时类中的指定的方法

通过反射,调用类中的方法,通过Method类完成。步骤:

  1. 通过Class类的getMethod(String name,Class…parameterTypes)方法取得 一个Method对象,并设置此方法操作时所需要的参数类型。
  2. 之后使用Object invoke(Object obj, Object[] args)进行调用,并向方法中 传递要设置的obj对象的参数信息。

在这里插入图片描述

Object invoke(Object obj, Object … args)
说明:
Object 对应原方法的返回值,若原方法无返回值,此时返回null

? 若原方法若为静态方法,此时形参Object obj可为null

? 若原方法形参列表为空,则Object[] argsnull
? 若原方法声明为private,则需要在调用此invoke()方法前,显式调用 方法对象的setAccessible(true)方法,将可访问private的方法。

代码演示:

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java"> <span style="color:#6272a4">/*
    如何操作运行时类中的指定的方法 -- 需要掌握
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">testMethod</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>

        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//创建运行时类的对象</span>
        Person p <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>Person<span style="color:#999999">)</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">/*
        1.获取指定的某个方法
        getDeclaredMethod():参数1 :指明获取的方法的名称  参数2:指明获取的方法的形参列表
         */</span>
        Method show <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredMethod</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"show"</span><span style="color:#999999">,</span> String<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//2.保证当前方法是可访问的</span>
        show<span style="color:#999999">.</span><span style="color:#8be9fd">setAccessible</span><span style="color:#999999">(</span><span style="color:#8be9fd">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">/*
        3. 调用方法的invoke():参数1:方法的调用者  参数2:给方法形参赋值的实参
        invoke()的返回值即为对应类中调用的方法的返回值。
         */</span>
        Object returnValue <span style="color:#6272a4">=</span> show<span style="color:#999999">.</span><span style="color:#8be9fd">invoke</span><span style="color:#999999">(</span>p<span style="color:#999999">,</span><span style="color:#f1fa8c">"CHN"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#6272a4">//String nation = p.show("CHN");</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>returnValue<span style="color:#999999">)</span><span style="color:#999999">;</span>

        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"*************如何调用静态方法*****************"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">// private static void showDesc()</span>

        Method showDesc <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredMethod</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"showDesc"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        showDesc<span style="color:#999999">.</span><span style="color:#8be9fd">setAccessible</span><span style="color:#999999">(</span><span style="color:#8be9fd">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#6272a4">//如果调用的运行时类中的方法没有返回值,则此invoke()返回null</span>
<span style="color:#6272a4">//        Object returnVal = showDesc.invoke(null);</span>
        Object returnVal <span style="color:#6272a4">=</span> showDesc<span style="color:#999999">.</span><span style="color:#8be9fd">invoke</span><span style="color:#999999">(</span>Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>returnVal<span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#6272a4">//null</span>

    <span style="color:#999999">}</span>


</code></span></span></span>

3. 调用运行时类中的指定的构造器

代码演示:

<span style="color:#000000"><span style="background-color:#282a36"><span style="color:pink"><code class="language-java">  <span style="color:#6272a4">/*
    如何调用运行时类中的指定的构造器
     */</span>
    <span style="color:#999999">@Test</span>
    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">void</span> <span style="color:#8be9fd">testConstructor</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ff79c6">throws</span> Exception <span style="color:#999999">{</span>
        Class clazz <span style="color:#6272a4">=</span> Person<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//private Person(String name)</span>
        <span style="color:#6272a4">/*
        1.获取指定的构造器
        getDeclaredConstructor():参数:指明构造器的参数列表
         */</span>

        Constructor constructor <span style="color:#6272a4">=</span> clazz<span style="color:#999999">.</span><span style="color:#8be9fd">getDeclaredConstructor</span><span style="color:#999999">(</span>String<span style="color:#999999">.</span><span style="color:#ff79c6">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//2.保证此构造器是可访问的</span>
        constructor<span style="color:#999999">.</span><span style="color:#8be9fd">setAccessible</span><span style="color:#999999">(</span><span style="color:#8be9fd">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

        <span style="color:#6272a4">//3.调用此构造器创建运行时类的对象</span>
        Person per <span style="color:#6272a4">=</span> <span style="color:#999999">(</span>Person<span style="color:#999999">)</span> constructor<span style="color:#999999">.</span><span style="color:#8be9fd">newInstance</span><span style="color:#999999">(</span><span style="color:#f1fa8c">"Tom"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#8be9fd">println</span><span style="color:#999999">(</span>per<span style="color:#999999">)</span><span style="color:#999999">;</span>

    <span style="color:#999999">}</span>

<span style="color:#999999">}</span>

</code></span></span></span>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/660962.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringMVC系列-1 使用方式和启动流程

背景 SpringMVC作为SSM组件之一&#xff0c;Java开发有必要了解SpringMVC是如何被集成到Spring框架以及整个项目的启动流程。本文以Tomcat作为Servlet容器进行介绍&#xff0c;默认认为读者使用过Tomcat且对Tomcat内部组件有足够的理解。 1.启动流程 当Tomcat被部署到服务器…

node的安装配置

这个是官网下载地址 推荐选择下载LTS版本的下载&#xff0c;不推荐下载最新版的&#xff0c;下载完成之后&#xff0c;在安装过程中更改安装路径默认是c盘的&#xff0c;如果c盘空间大当我没说&#xff0c;接着一路傻瓜式安装默认点击下一步&#xff0c;直到安装完成。 创建文…

chatgpt赋能python:Python怎样设置字体大小

Python 怎样设置字体大小 作为一名Python工程师&#xff0c;我们经常需要在论文或文档中使用Python进行数据分析&#xff0c;数据可视化和数据科学等方面的工作。在这些文档中&#xff0c;字体大小的设置是非常重要的&#xff0c;因为它直接影响阅读体验和文档的可读性。因此&…

幸运九宫格抽奖码九宫格抽奖系统独立版源码php修复版

&#x1f389; 有需要的朋友记得关赞评&#xff0c;文章底部来交流&#xff01;&#xff01;&#xff01; &#x1f389; ✨ 源码介绍 1.本程序在PHP5.3 ~ PHP5.6环境 2.后台访问地址&#xff1a;域名/admin/index.php U:admin P:admin 手动安装 1.导入1776.sql到数据库 …

【文件操作与IO】认识文件

目录 认识文件 狭义上的文件 广义上的文件 树形结构组织和目录 文件路径&#xff08;Path&#xff09; 其他知识 认识文件 狭义上的文件 硬盘上保存的数据&#xff0c;都是“文件”来组织的&#xff0c;本质上都是二进制或是字符组织的数组&#xff0c;被打包成一个文件…

Git学习笔记(上篇)

导航小助手 一、Git初识 1.1 场景 1.2 版本控制器 1.3 注意事项 二、Git安装 2.1 Linux-centos 2.2 Linux-ubuntu 三、Git基本操作 3.1 创建Git本地仓库 3.2 配置Git本地仓库 3.3 认识工作区、暂存区、版本库 3.3.1 添加文件—场景一 3.3.2 添加文件—场景二 3.4…

chatgpt赋能python:Python怎么重新执行代码

Python怎么重新执行代码 如果你正在学习编程&#xff0c;你可能已经意识到Python是一种非常流行的语言。Python是一种如此强大的编程语言&#xff0c;可以应用于许多领域&#xff0c;从数据分析到游戏开发&#xff0c;甚至是Web开发。但是&#xff0c;在编写Python代码时&…

026.【图形结构算法】

1. 图的定义 树形结构用于描述节点和节点之间的层次关系&#xff0c;而图形结构用于描述两个顶点之间是否连通的关系。在计算机科学中&#xff0c;图形结构是最灵活的数据结构之一&#xff0c;很多问题都可以使用图来求解。 无向图是每条边都没有方向的图&#xff0c;同一个边…

chatgpt赋能python:重新安装Python——让你的编程之路更畅通

重新安装Python——让你的编程之路更畅通 Python是一种高级编程语言&#xff0c;广泛应用于软件开发、数据科学、机器学习等领域&#xff0c;因其易学易用、拥有丰富的第三方库和社区支持而备受程序员们的喜爱。但是&#xff0c;有时候你可能会遇到无法解决的Python问题&#…

Linux 4.10当中将带来深远影响的三项小改变

Linux的演进永不停歇。Linus Torvalds一直在努力工作&#xff0c;希望能够在新的内核版本当中(4.11)融入更多变化。不过在目前的Linux 4.10中&#xff0c;我们同样发现了三组能够有效提升性能并实现多种前所未有功能集的变更。 下面&#xff0c;我们将共同了解这些可能对您、您…

0006-TIPS-2020-hxp-kernel-rop : bypass-KASLR-with-offset_leak

内核默认加载地址&#xff08;不开启KASLR&#xff09; kernel text mapping 在内核linux-5.9/Documentation/x86/x86_64/mm.rst文档中记录了 x86_64虚拟地址空间布局 其中0xffffffff80000000~0xffffffff9fffffff用于存放内核代码段、全局变量、BSS等 ffffffff80000000 | -…

华为OD机试真题 JavaScript 实现【字符串序列判定】【2022Q4 100分】,附详细解题思路

一、题目描述 输入两个字符串a和b&#xff0c;都只包含英文小写字母。a长度<100&#xff0c;b长度<500,000。 判定a是否是b的有效子串。 判定规则&#xff1a; a中的每个字符在b中都能找到&#xff08;可以不连续&#xff09;&#xff0c;且a在b中字符的前后顺序与a中…

【SQL应知应会】分析函数的点点滴滴(一)

欢迎来到爱书不爱输的程序猿的博客, 本博客致力于知识分享&#xff0c;与更多的人进行学习交流 本文收录于SQL应知应会专栏,本专栏主要用于记录对于数据库的一些学习&#xff0c;有基础也有进阶&#xff0c;有MySQL也有Oracle 分析函数的点点滴滴 1.什么是分析函数&#xff1a;…

万字详解常用设计模式

本文是博主在工作中对常用设计模式的使用经验总结归纳而来分享给大家。 设计模式一共有23种&#xff0c;本文讲解涉及如下&#xff1a; 责任链模式 模板方法模式 发布订阅模式 策略模式 三大分类 业界一般将设计模式分为三大类&#xff1a; 创建型模式&#xff1a;对类的实…

chatgpt赋能python:Python怎样调字体大小以及优化网站SEO

Python怎样调字体大小以及优化网站SEO 在现代网络时代&#xff0c;网站的排名和SEO越来越受到关注。有一些关键词和技巧可以用来在搜索引擎排名中获得好的位置。其中一个技术是调整字体大小。在本文中&#xff0c;我们将深入探讨如何使用Python调整字体大小&#xff0c;并进一…

chatgpt赋能python:Python如何随机产生多个随机数?

Python如何随机产生多个随机数&#xff1f; Python是一种高级编程语言&#xff0c;它的随机数生成器是其强大的功能之一。在本文中&#xff0c;我们将学习如何使用Python随机数生成器生成多个随机数。 基本概念&#xff1a;随机数生成器 随机数生成器是一种算法或物理设备&a…

【Python】APScheduler定时调度库

文章目录 APScheduler是什么功能特点四大组件触发器[triggers]date 一次性触发器interval 间隔触发器cron 周期触发器 任务存储器[JobStore]MemoryJobStoreMongoDBJobStoreRedisJobStore示例代码 RethinkDBJobStoreSQLAlchemyJobStoreZooKeeperJobStore 执行器[executors]线程池…

chatgpt赋能python:Python如何隐藏进程

Python如何隐藏进程 介绍 进程是指运行中的程序在操作系统中的一个实例。在计算机系统中&#xff0c;进程通常都可以被用户或者其他程序所看到。然而&#xff0c;有时候我们需要隐藏进程&#xff0c;比如保护敏感信息或者防止恶意攻击。 Python是一种高级编程语言&#xff0…

Maven学习笔记(SSM 整合伪分布式案例)

目录 第一节 创建工程&#xff0c;引入依赖 1 创建工程 ①工程清单 ②工程间关系 2、各工程 POM 配置 ①父工程 ②Mybatis 逆向工程 ③环境依赖工程 ④工具类工程 ⑤实体类工程 ⑥组件工程 ⑦Web 工程 第二节 搭建环境&#xff1a;持久化层 1、物理建模 2、Myba…

leetcode算法 -- 数组

1 数组 常见的数组算法有双指针&#xff0c;滑动窗口&#xff0c;二分查找和分冶。 2 双指针 核心的思路&#xff1a;使用两个指针&#xff0c;一个从头开始索引&#xff0c;一个从尾开始索引。 2.1 两数之和ii 167 给你一个下标从 1 开始的整数数组 numbers &#xff0c;该…