反射获取构造方法并使用

Student类:

package com.reflect_02;

public class Student {
    private String name;
    int age;
    public String address;

    //构造方法:1个私有,1个默认,2个公开
    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}
View Code
Constructor<?>[] getConstructors() 返回包含一个数组 Constructor对象反射由此表示的类的所有公共构造类对象。
public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        //Constructor<?>[] getConstructors()
        //返回包含一个数组 Constructor对象反射由此表示的类的所有公共构造类对象。
        Constructor<?>[] cons = c.getConstructors();
        for (Constructor con : cons){
            System.out.println(con);
        }
    }
}

此方法的运行结果:返回的构造方法 都是public的

Constructor<?>[] getDeclaredConstructors() 返回一个反映 Constructor对象表示的类声明的所有Constructor对象数组类 。

public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        //Constructor<?>[] getDeclaredConstructors()
        //返回一个反映 Constructor对象表示的类声明的所有Constructor对象的数组类 。
        Constructor<?>[] cons = c.getDeclaredConstructors();
        for (Constructor con : cons){
            System.out.println(con);
        }
    }
}

该方法的运行结果:所有的构造方法都被返回

Constructor<T> getConstructor(Class <?>... parameterTypes)  返回一个Constructor对象,该对象反映 Constructor对象表示的类的指定的公共类函数

参数:你要获取的构造方法的参数的个数和数据类型对应的字节码文件对象

Constructor提供了一个类的单个构造函数的信息和访问。

T newInstance(Object... initargs) 使用此 Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例

public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        //Constructor<T> getConstructor(Class <?>... parameterTypes)
        //返回一个Constructor对象,该对象反映 Constructor对象表示的类的指定的公共类函数。

        Constructor<?> con = c.getConstructor();

        //Constructor提供了一个类的单个构造函数的信息和访问。
        //T newInstance(Object... initargs) 使用此 Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例。

        Object o = con.newInstance();
        System.out.println(o);
    }
}

使用getConstructor()方法来返回一个Constructor对象,通过Constructor对象调用newInstance(Object... initargs)方法来获取一个构造方法,结果如下:(为传递参数,使用了Constructor对象表示的无参构造方法,创造了一个Student实例)

C

 同样,getConstructor()方法只能获取公共的构造方法,所以需要使用

Constructor<T> getDeclaredConstructor(Class <?>... parameterTypes) 返回一个 Constructor对象,该对象反映 Constructor对象表示的类或接口的指定类函数

public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        //Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
        //返回一个 Constructor对象,该对象反映 Constructor对象表示的类或接口的指定 类函数。

        Constructor<?> con = c.getDeclaredConstructor(String.class,int.class);
        Object obj = con.newInstance("林青霞",30);
        System.out.println(obj);
    }
}

运行上面的代码:出现异常

出现异常的原因在于,调用的构造方法是private或默认的修饰符修饰的,而私有或默认的访问权限是不能被直接拿来操作的。

但是,在反射中可以使用一个方法来进行暴力反射,从而对被私有权限的构造方法进行操作

public void setAccessible(boolean flag):将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。

修改代码:

public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        //Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
        //返回一个 Constructor对象,该对象反映 Constructor对象表示的类或接口的指定 类函数。

        Constructor<?> con = c.getDeclaredConstructor(String.class,int.class);
     con,setAccessible(true); Object obj
= con.newInstance("林青霞",30); System.out.println(obj); } }

修改代码后的运行结果:

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/13170519.html