反射-class对象获取Constructor和Class对象获取Method
class对象获取Constructor
获取构造方法们
- Constructor<?>[] getConstructors():获取所有的构造方法
- Constructor<T> getConstructor(类<?>... parameterTypes):获取指定的构造方法
- Constructor<T> getDeclaredConstrutor(类<?>... parameterTypes)
- Constructor<?> getDeclaredConstructors()
Constructor:构造方法
创建对象:
T newInstance(Object... initargs)
如果创建使用空参构造方法创建对象 操作可以简化:Class对象的newInstance方法
代码:
public static void main(String[] args) throws Exception { Class person=Person.class; /* Constructor<?>[] getConstructors() //获取所有的构造方法 Constructor<T> getConstructor(类<?>... parameterTypes)//获取指定的构造方法 Constructor<T> getDeclaredConstrutor(类<?>... parameterTypes) Constructor<?> getDeclaredConstructors() */ //Constructor<?>[] getConstructors()获取所有的构造方法 Constructor[] con = person.getConstructors(); for (Constructor constructor : con) { System.out.println(constructor); } System.out.println("------------------------------"); // Constructor<T> getConstructor(类<?>... parameterTypes)//获取指定的构造方法 Constructor ct = person.getConstructor(String.class, int.class); System.out.println(ct); //创建对象 Object obj = ct.newInstance("张三", 10); System.out.println(obj); System.out.println("------------------------------"); //Class对象的newInstance方法 只可以创建无参构造 Person person1 = Person.class.newInstance(); System.out.println(person1); }
运行结果
Class对象获取Method
3.获取成员方法们
- Method[] getMethods()
- Method getMethod(String name,类<?>... parameterTypes)
- Method[] getDeclaredMethods()
- Method getDeckaredMethod(String name,类<?>... paramterTypes)
Method:方法
执行方法:
Object invoke(Object obj,Object... args)
获取方法名称:
String getName:获取方法名
代码:
public static void main(String[] args) throws Exception { Class person = Person.class; /* 获取成员方法们 Method[] getMethods()//获取所有的public方法 Method getMethod(String name,类<?>... parameterTypes)//获取指定的public方法 Method[] getDeclaredMethods() Method getDeckaredMethod(String name,类<?>... paramterTypes) */ Method[] me = person.getMethods(); for (Method method : me) { System.out.println(method); } System.out.println("---------------------"); Method eat = person.getMethod("eat"); System.out.println(eat); Person p = new Person(); //Object invoke(Object obj,Object... args)执行方法 Object in = eat.invoke(p); System.out.println(in); System.out.println("---------------------"); Method[] me1 = person.getMethods(); for (Method method : me1) { System.out.println(method); //String getName:获取方法名 String name = method.getName(); System.out.println(name); } }
运行结果:
练习-反射
需求:写一个“框架”,不能改变类的任何代码的前提下,可以帮我们创建任意类的对象 并且执行其中任意方法
实现:
1.配置文件
2.反射
步骤:
1. 将需要创建的对象的全类名和需要执行的方法定义在配置文件中
2.在程序中加载读取配置文件
3.使用反射技术来加载类文件内存
4.创建对象
5.执行方法
代码:
学生类:
public class Student { public void sleep(){ System.out.println("sleep~~"); } }
Person类:
public class Person { public String name; public int age; private int a; int ab; public Person(String name, int age) { this.name = name; this.age = age; } public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } //成员方法 public void eat(){ System.out.println("吃东西"); } }
配置类:
className=bai.demoReflect.Student
methodName=sleep
测试类:
public static void main(String[] args) throws Exception { //1.加载配置文件 //1.1创建Properties对象 Properties pro=new Properties(); //1.2加载配置文件 转换为一个集合 //1.2.1获取class目录下的配置文件 ClassLoader classLoader = ReflectTest.class.getClassLoader(); InputStream is = classLoader.getResourceAsStream("pro.properties"); pro.load(is); //2.获取配置文件中定义的数据 String className = pro.getProperty("className"); String methodName = pro.getProperty("methodName"); //3.加载该类进内存 Class cls = Class.forName(className); //4.创建对象 Object obj = cls.newInstance(); //5.获取方法对象 Method method = cls.getMethod(methodName); //6.执行方法 method.invoke(obj); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本