代码:
package com.itheima;
import java.lang.reflect.Constructor;
public class ReflectTest {
private ReflectTest(){
System.out.println("空参的构造函数被调用");
}
private ReflectTest(String str){
System.out.println(str);
}
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Class clazz = ReflectTest.class;
Constructor con = clazz.getDeclaredConstructor(null);
Constructor con2 = clazz.getDeclaredConstructor(String.class);
con.setAccessible(true);
con2.setAccessible(true);
Object obj = con.newInstance(null);
Object obj2 = con2.newInstance("hello,world");
}
}
运行结果:
空参的构造函数被调用
hello,world
|
|