查看getConstructor方法的源代码如下:
public Constructor<T> getConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
// be very careful not to change the stack depth of this
// checkMemberAccess call for security reasons
// see java.lang.SecurityManager.checkMemberAccess
checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
return getConstructor0(parameterTypes, Member.PUBLIC);
}
构造方法需要是public的,你才能反射的构造方法
故改为:
public Test2(String name){
this.name=name;
} |