java类反射暴力反射

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//暴力反射使用私有方法
public class ReflectDemo3 {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Student> studentClass = Student.class;
        Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor(String.class);
        //暴力反射,强行使用私有构造器
        declaredConstructor.setAccessible(true);
        Student student = declaredConstructor.newInstance("郭奉孝");
        System.out.println(student);
    }
}


posted @ 2021-05-03 22:05  code-G  阅读(310)  评论(0编辑  收藏  举报