用getDeclaredConstructor(Class<?>... parameterTypes)
然后用Constructor父类的setAccessible(true)---就是暴力反射
例如:
import java.lang.reflect.*;
public class SecertExercise {
public static void main(String[] args)throws Exception {
Constructor constructor = Abc.class.getDeclaredConstructor(String.class,int.class);
//设置对访问权限的释放
constructor.setAccessible(true);
Abc abc =(Abc)constructor.newInstance("689",12);
abc.run();
}
}
class Abc{
String name ;
int age;
private Abc(String name,int age){
this.name=name;
this.age=age;
}
void run(){
System.out.println("haha");
}
} |