Java反射知识点学习笔记
原创 已于 2025-04-17 21:33:02 修改 · 粉丝可见 · 714 阅读 · 19 · 5 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/147292144
目录
[TOC]
Java反射是一种强大的编程机制,它允许程序在运行时检查和操作类、接口、字段和方法等元素。
一、定义
Java反射是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性。这种动态获取信息以及动态调用对象方法的功能被称为Java语言的反射机制。( 反射允许对成员变量,成员方法和构造方法的信息进行编程访问 )
二、获取class对象的三种方式
1、Class.forName(“全类名”)

源代码阶段使用 Class.forName(“”) 方式获取反射对象。
2、类名.class

当类名加载到了内存中,使用 类名.class 方式获取class反射对象。
3、对象.getClass()

当内存中 new 了一个类对象,处于运行阶段。则使用 对象.getClass() 方式获取反射对象。
三、案例
1、获取 class 反射对象三种方式
假设我们有 Student 对象
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.angindem.myreflect1; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } }
|
代码案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.angindem.myreflect1; public class MyReflectDemo1 { public static void main(String[] args) throws ClassNotFoundException {
Class sClass1 = Class.forName("com.angindem.myreflect1.Student"); System.out.println(sClass1);
Class sClass2 = Student.class; System.out.println(sClass2); System.out.println(sClass1 == sClass2);
Student s = new Student("angindem", 18); Class sClass3 = s.getClass(); System.out.println(sClass3); System.out.println(sClass1 == sClass3); } }
|
2、利用反射获取构造方法
假设我们有 Student 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.angindem.myreflect2; import lombok.Data; import lombok.NoArgsConstructor; @Data public class Student { private String name; private int age; private Student(String name, int age) { this.name = name; this.age = age; } public Student() { } public Student(String name) { this.name = name; } protected Student(int age) { this.age = age; } }
|
代码案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package com.angindem.myreflect2; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class MyReflectDemo { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> clazz = Class.forName("com.angindem.myreflect2.Student");
Constructor<?> con1 = clazz.getDeclaredConstructor();
Constructor<?> con2 = clazz.getDeclaredConstructor(String.class);
Constructor<?> con3 = clazz.getDeclaredConstructor(int.class);
Constructor<?> con4 = clazz.getDeclaredConstructor(String.class, int.class);
con4.setAccessible(true); Student stu = (Student) con4.newInstance("angindem", 18); System.out.println(stu); } }
|
扩展:关于 Modifier 的 常量字段值
| 修饰符 |
值 |
十六进制表示 |
描述 |
| public |
1 |
0x0001 |
表示该成员(类、方法、字段等)对所有类都可见。 |
| private |
2 |
0x0002 |
表示该成员只能在定义它的类内部访问。 |
| protected |
4 |
0x0004 |
表示该成员在定义它的类、同一包中的类以及所有子类中可见。 |
| default |
0 |
0x0000 |
表示该成员在定义它的类和同一包中的类中可见(没有显式修饰符)。 |
| abstract |
1024 |
0x0400 |
表示该类是抽象类,不能被实例化。 |
| final |
16 |
0x0010 |
表示该类不能被继承,或者该方法不能被重写,或者该字段是常量。 |
| interface |
512 |
0x0200 |
表示该类是一个接口。 |
| static |
8 |
0x0008 |
表示该字段或方法是静态的,属于类本身而不是类的实例。 |
| transient |
128 |
0x0080 |
表示该字段在序列化时不会被序列化。 |
| volatile |
64 |
0x0040 |
表示该字段的值可能会被多个线程同时访问,每次访问都需要从主内存中读取。 |
| synchronized |
32 |
0x0020 |
表示该方法是同步的,同一时间只能被一个线程访问。 |
| native |
256 |
0x0100 |
表示该方法是本地方法,其实现用非 Java 语言编写。 |
3、利用反射获取成员变量
假设我们有 Student 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.angindem.myreflect3; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class Student { private String name; private int age; public String gender; public Student(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } }
|
代码案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.angindem.myreflect3; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; public class MyReflectDemo { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
Class<?> clazz = Class.forName("com.angindem.myreflect3.Student");
Field name = clazz.getDeclaredField("name"); System.out.println(name);
int modifiers = name.getModifiers(); System.out.println(modifiers);
String n = name.getName(); System.out.println(n);
Class<?> type = name.getType(); System.out.println(type);
Student stu = new Student("angindem", 18, "男"); name.setAccessible(true); Object value = name.get(stu); System.out.println(value);
name.set(stu, "ang"); System.out.println(stu); } }
|
4、利用反射获取成员方法
假设我们有 Student 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.angindem.myreflect4; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void sleep(){ System.out.println("sleep睡觉"); } private void eat(String food){ System.out.println("在吃 " + food); } }
|
代码案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.angindem.myreflect4; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class MyReflectDemo1 { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> clazz = Class.forName("com.angindem.myreflect4.Student");
Method m = clazz.getDeclaredMethod("eat", String.class); System.out.println(m);
int modifiers = m.getModifiers(); System.out.println(modifiers);
String name = m.getName(); System.out.println(name);
Class<?>[] parameterTypes = m.getParameterTypes(); for (Class<?> parameterType : parameterTypes){ System.out.println(parameterType); }
Class<?>[] exceptionTypes = m.getExceptionTypes(); for (Class<?> exceptionType : exceptionTypes){ System.out.println(exceptionType); }
Class<?> returnType = m.getReturnType(); System.out.println(returnType);
Student s = new Student();
m.setAccessible(true); String result = (String) m.invoke(s, "汉堡包"); System.out.println(result); } }
|