反射通过私有方法来构架对象
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);
}
}

浙公网安备 33010602011771号