JavaSE-24.2.4【反射获取成员方法并使用】
1 package day15.lesson2; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 7 /* 8 2.8 反射获取成员方法并使用 9 10 Class类获取成员方法对象的方法 11 12 Method[] getMethods() 返回所有公共成员方法对象的数组,包括继承的 13 返回一个包含方法对象的数组,方法对象反映由该 Class对象表示的类或接口的所有公共方法, 14 包括由类或接口声明的对象以及从超类和超级接口继承的类 15 16 Method[] getDeclaredMethods() 返回所有成员方法对象的数组,不包括继承的 17 返回一个包含方法对象的数组,方法对象反映由Class对象表示的类或接口的所有声明方法, 18 包括public,protected,default(package)访问和私有方法,但不包括继承方法 19 20 Method getMethod(String name, Class<?>... parameterTypes) 返回单个公共成员方法对象 21 返回一个方法对象,该对象反映由该Class对象表示的类或接口的指定公共成员方法 22 23 Method getDeclaredMethod(String name, Class<?>... parameterTypes) 返回单个成员方法对象 24 返回一个方法对象,它反映此表示的类或接口的指定声明的方法 Class对象 25 26 Method类用于执行方法的方法 27 Object invoke(Object obj,Object... args) 调用obj对象的成员方法,参数是args,返回值是Object类型 28 */ 29 public class Demo6Reflect { 30 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { 31 Class<?> c = Class.forName("day15.lesson2.Student"); 32 33 34 Method[] methods = c.getMethods(); 35 for (Method m: methods){ 36 System.out.println(m); 37 } 38 /* 39 public java.lang.String day15.lesson2.Student.toString() 40 public void day15.lesson2.Student.method2(java.lang.String) 41 public java.lang.String day15.lesson2.Student.method3(java.lang.String,int) 42 public void day15.lesson2.Student.method1() 43 public final void java.lang.Object.wait() throws java.lang.InterruptedException 44 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException 45 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException 46 public boolean java.lang.Object.equals(java.lang.Object) 47 public native int java.lang.Object.hashCode() 48 public final native java.lang.Class java.lang.Object.getClass() 49 public final native void java.lang.Object.notify() 50 public final native void java.lang.Object.notifyAll() 51 */ 52 System.out.println(); 53 54 55 Method[] declaredMethods = c.getDeclaredMethods(); 56 for (Method m: declaredMethods){ 57 System.out.println(m); 58 } 59 /* 60 public java.lang.String day15.lesson2.Student.toString() 61 private void day15.lesson2.Student.function() 62 public void day15.lesson2.Student.method2(java.lang.String) 63 public java.lang.String day15.lesson2.Student.method3(java.lang.String,int) 64 public void day15.lesson2.Student.method1() 65 */ 66 System.out.println(); 67 68 69 Method method = c.getMethod("method1"); //获取成员方法对象,传入方法名及原方法所需参数的类型 70 71 /*Student s = new Student(); 72 s.method1();*/ 73 //通过反射实现上述 74 Constructor<?> con = c.getConstructor(); //获取无参构造 75 Object obj = con.newInstance(); //创建对象 76 77 /* 78 Method类:在类或接口上提供有关单一方法的信息和访问权限 79 Object invoke(Object obj, Object... args) 在具有指定参数的指定对象上调用此方法对象表示的基础方法 80 obj:调用方法的对象 81 args:方法需要的参数 82 */ 83 method.invoke(obj); //method 84 } 85 }
1 class Student{ 2 //成员变量 3 private String name; //私有 4 int age; //默认 5 public String address; //公共 6 7 //构造方法 8 public Student() { //公共 9 } 10 11 public Student(String name, int age, String address) { //公共 12 this.name = name; 13 this.age = age; 14 this.address = address; 15 } 16 17 private Student(String name){ //私有 18 this.name = name; 19 } 20 21 Student(String name, int age){ //默认 22 this.name = name; 23 this.age = age; 24 } 25 26 //成员方法 27 private void function(){ //私有 28 System.out.println("function"); 29 } 30 31 public void method1(){ //公共 32 System.out.println("method"); 33 } 34 35 public void method2(String s){ //公共 36 System.out.println("method:" + s); 37 } 38 39 public String method3(String s, int i){ //公共 40 return s + "," + i; 41 } 42 43 @Override 44 public String toString() { //公共 45 return "Student{" + 46 "name='" + name + '\'' + 47 ", age=" + age + 48 ", address='" + address + '\'' + 49 '}'; 50 } 51 }
1 package day15.lesson2; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 7 /* 8 2.9 反射获取成员方法并使用练习 9 10 通过反射获取成员方法并调用 11 12 通过反射实现如下: 13 Student s = new Student(); 14 s.method1(); 15 s.method2("tom"); 16 String str = s.method3("tom, 23"); 17 sout(str); 18 s.function(); 19 */ 20 public class Demo7Reflect { 21 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { 22 Class<?> c = Class.forName("day15.lesson2.Student"); 23 24 // Student s = new Student(); 25 Constructor<?> con = c.getConstructor(); 26 Object obj = con.newInstance(); 27 28 // s.method1(); 29 Method method1 = c.getMethod("method1"); 30 method1.invoke(obj); //method 31 32 // s.method2("tom"); 33 Method method2 = c.getMethod("method2", String.class); 34 method2.invoke(obj, "tom"); //method:tom 35 36 // String str = s.method3("tom, 23"); 37 Method method3 = c.getMethod("method3", String.class, int.class); 38 Object o = method3.invoke(obj, "tom", 23); 39 System.out.println(o); //tom,23 40 41 // s.function(); 42 Method function = c.getDeclaredMethod("function"); //私用只能使用xxxDeclaredxxx 43 function.setAccessible(true); //私有必须暴力反射,不然无法实现会报错 44 function.invoke(obj); //function 45 } 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!