Java-反射机制(超详解)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Java反射机制概述



前言

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

一、Java反射机制概述

1. Java Reflection

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

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

2. 动态语言 vs 静态语言

1动态语言

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

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

2静态语言

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

3Java反射机制研究及应用

? 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对象

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

三、获取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>
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java