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

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

        Constructor<?> con = c.getDeclaredConstructor(String.class);
        //取消访问检查
        con.setAccessible(true);
        Object obj = con.newInstance("林青霞");
        System.out.println(obj);
    }
}

运行结果:

posted @ 2020-06-20 22:29  硬盘红了  阅读(82)  评论(0编辑  收藏  举报