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反射机制提供的功能
- 在运行时判断任意一个对象所属的类
- 在运行时构造任意一个类的对象
- 在运行时判断任意一个类所具有的成员变量和方法
- 在运行时获取泛型信息 在运行时调用任意一个对象的成员变量和方法
- 在运行时处理注解 生成动态代理
? 反射相关的主要API
- java.lang.Class:代表一个类
- java.lang.reflect.Method:代表类的方法
- java.lang.reflect.Field:代表类的成员变量
- java.lang.reflect.Constructor:代表类的构造器 ? … …
二、 Class类的理解
1. 类的加载过程
? 程序经过
javac.exe
命令以后,会生成一个或多个字节码文件(.class
结尾)。
接着我们使用java.exe
命令对某个字节码文件进行解释运行。相当于将某个字节码文件加载到内存中。此过程就称为类的加载。加载到内存中的类,我们就称为运行时类,此运行时类,就作为Class
的一个实例。
? 换句话说,
Class
的实例就对应着一个运行时类。
? 加载到内存中的运行时类,会缓存一定的时间。在此时间之内,我们可以通过不同的方式来获取此运行时类。
当程序主动使用某个类时,如果该类还未被加载到内存中,则系统会通过如下三个步骤来对该类进行初始化。
? 类的加载:将
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>
? 类的主动引用(一定会发生类的初始化)
- 当虚拟机启动,先初始化
main
方法所在的类new
一个类的对象- 调用类的静态成员(除了final常量)和静态方法
- 使用
java.lang.reflect
包的方法对类进行反射调用- 当初始化一个类,如果其父类没有被初始化,则先会初始化它的父类
? 类的被动引用(不会发生类的初始化)
- 当访问一个静态域时,只有真正声明这个域的类才会被初始化
- 当通过子类引用父类的静态变量,不会导致子类初始化
- 通过数组定义类引用,不会触发此类的初始化
- 引用常量不会触发此类的初始化(常量在链接阶段就存入调用类的常量池中了)
? 类加载的作用:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的java.lang.Class对象,作为 方法区中类数据的访问入口。
? 类缓存:标准的JavaSE类加载器可以按要求查找类,但一旦某个类被加载到类加载器 中,它将维持加载(缓存)一段时间。不过JVM垃圾回收机制可以回收这些Class对象。
? 不同类型的类的加载器:
<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()
方法
要求:
- 类必须有一个无参数的构造器。
- 类的构造器的访问权限需要足够。
? 难道没有无参的构造器就不能创建对象了吗?
不是!只要在操作的时候明确的调用类中的构造器,并将参数传递进去之后,才可以实例化操作。
步骤如下:
- 通过
Class
类的getDeclaredConstructor(Class … parameterTypes)
取得本类的指定形参类型的构造器- 向构造器的形参中传递一个对象数组进去,里面包含了构造器中所需的各个参数。
- 通过
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 对象所表示的类或接口的public 的Field |
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方法的使用
?
Method
和Field
、Constructor
对象都有setAccessible()
方法。
?
setAccessible
启动和禁用访问安全检查的开关。
? 参数值为
true
则指示反射的对象在使用时应该取消Java语言访问检查。
? 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被 调用,那么请设置为
true
,使得原本无法访问的私有成员也可以访问,参数值为false
则指示反射的对象应该实施Java语言访问检查。
1. 调用运行时类中指定的属性
在反射机制中,可以直接通过
Field
类操作类中的属性,通过Field
类提供的set()
和get()
方法就可以完成设置和取得属性内容的操作。
方法 | 作用 |
---|---|
public Field getField(String name) | 返回此Class 对象表示的类或接口的指定的public 的Field |
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
类完成。步骤:
- 通过
Class
类的getMethod(String name,Class…parameterTypes)
方法取得 一个Method
对象,并设置此方法操作时所需要的参数类型。- 之后使用
Object invoke(Object obj, Object[] args)
进行调用,并向方法中 传递要设置的obj
对象的参数信息。
Object invoke(Object obj, Object … args)
说明:
?Object
对应原方法的返回值,若原方法无返回值,此时返回null
? 若原方法若为静态方法,此时形参
Object obj
可为null
? 若原方法形参列表为空,则
Object[] args
为null
? 若原方法声明为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>