其他分享
首页 > 其他分享> > 反射通过私有方法来构架对象

反射通过私有方法来构架对象

作者:互联网

package Fanshe;
//需求 实现Student s=new Student("李清霞");
//System.out.println(s)
import java.lang.reflect.Constructor;
public class Fanshedeom2 {
public static void main(String[] args) throws Exception{
Class<?> clss = Class.forName("Fanshe.Student");
Constructor<?> stu = clss.getDeclaredConstructor(String.class);
//不能对私有方法构造对象。如果需要 必须使用暴力反射
stu.setAccessible(true);//将值设为true,系统构造时,取消检测
Object obj = stu.newInstance("李海霞");
System.out.println(obj);
}
}

标签:反射,String,私有,System,stu,Student,构架,Fanshe,out
来源: https://www.cnblogs.com/koushijun575/p/15566177.html