当前位置: 代码迷 >> 综合 >> 反射获取构造方法并使用【应用】
  详细解决方案

反射获取构造方法并使用【应用】

热度:71   发布时间:2024-02-19 12:48:09.0

Class类获取构造方法对象的方法

  • 方法分类

    方法名 说明
    Constructor<?>[] getConstructors() 返回所有公共构造方法对象的数组
    Constructor<?>[] getDeclaredConstructors() 返回所有构造方法对象的数组
    Constructor<T> getConstructor(Class<?>... parameterTypes) 返回单个公共构造方法对象
    Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 返回单个构造方法对象
  • 示例代码

public class ReflectDemo01 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//获取Class对象Class<?> c = Class.forName("com.leon_02.Student");//Constructor<?>[] getConstructors() 返回一个包含 Constructor对象的数组, Constructor对象反映了由该 Class对象表示的类的所有公共构造函数
//        Constructor<?>[] cons = c.getConstructors();//Constructor<?>[] getDeclaredConstructors() 返回反映由该 Class对象表示的类声明的所有构造函数的 Constructor对象的数组Constructor<?>[] cons = c.getDeclaredConstructors();for(Constructor con : cons) {System.out.println(con);}System.out.println("--------");//Constructor<T> getConstructor(Class<?>... parameterTypes) 返回一个 Constructor对象,该对象反映由该 Class对象表示的类的指定公共构造函数//Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 返回一个 Constructor对象,该对象反映由此 Class对象表示的类或接口的指定构造函数//参数:你要获取的构造方法的参数的个数和数据类型对应的字节码文件对象Constructor<?> con = c.getConstructor();//Constructor提供了一个类的单个构造函数的信息和访问权限//T newInstance(Object... initargs) 使用由此 Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例Object obj = con.newInstance();System.out.println(obj);//        Student s = new Student();
//        System.out.println(s);}
}

Constructor类用于创建对象的方法

方法名 说明
T newInstance(Object...initargs) 根据指定的构造方法创建对象

2.4反射获取构造方法并使用练习1【应用】

  • 案例需求

    • 通过反射获取公共的构造方法并创建对象

  • 代码实现

    • 学生类

public class Student {//成员变量:一个私有,一个默认,一个公共private String name;int age;public String address;//构造方法:一个私有,一个默认,两个公共public Student() {}private Student(String name) {this.name = name;}Student(String name, int age) {this.name = name;this.age = age;}public Student(String name, int age, String address) {this.name = name;this.age = age;this.address = address;}//成员方法:一个私有,四个公共private void function() {System.out.println("function");}public void method1() {System.out.println("method");}public void method2(String s) {System.out.println("method:" + s);}public String method3(String s, int i) {return s + "," + i;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", address='" + address + '\'' +'}';}
}

测试类

public class ReflectDemo02 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//获取Class对象Class<?> c = Class.forName("com.leon_02.Student");//public Student(String name, int age, String address)//Constructor<T> getConstructor(Class<?>... parameterTypes)Constructor<?> con = c.getConstructor(String.class, int.class, String.class);//基本数据类型也可以通过.class得到对应的Class类型//T newInstance(Object... initargs)Object obj = con.newInstance("林青霞", 30, "西安");System.out.println(obj);}
}

反射获取构造方法并使用练习2【应用】

  • 案例需求

    • 通过反射获取私有构造方法并创建对象

  • 代码实现

    • 学生类:参见上方学生类

    • 测试类

public class ReflectDemo03 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//获取Class对象Class<?> c = Class.forName("com.leon_02.Student");//private Student(String name)//Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)Constructor<?> con = c.getDeclaredConstructor(String.class);//暴力反射//public void setAccessible(boolean flag):值为true,取消访问检查con.setAccessible(true);Object obj = con.newInstance("林青霞");System.out.println(obj);}
}

 

  相关解决方案