日期:2014-05-20  浏览次数:20854 次

求助:getConstructor时NoSuchMethodException
如题,对Constructor不理解,,求指点,代码如下:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ConstructorMe2 {

public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Class sampleClass=AA.class;
ConstructorMe2 c=new ConstructorMe2();

Constructor constructor= sampleClass.getConstructor(int.class);

constructor.newInstance(9);
}
}

class AA{
AA(int a){
System.out.println(a);
}
}

------解决方案--------------------
OH,知道了,你的AA的构造函数不是public的,要么改成public或者改成Constructor constructor = c.getDeclaredConstructor(int.class);