public class Test8 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Class<?> clazz=Class.forName("Apple");
System.out.println(clazz);
Constructor ctr=clazz.getConstructor(String.class);
Object obj=ctr.newInstance("我是一只用反射方式创造的苹果");
System.out.println(obj);
}
}
class Apple{
String str="我是一只普通苹果";
Apple(String str){
this.str=str;
}
public void say(String s){
System.out.println(str+":"+s);
}
}
这代码抛出了java.lang.NoSuchMethodException异常,可是我Apple类中是有带一个字符串参数的构造器啊,怎么会没有呢,还是说我传类名的时候传错了,可是打印class对象输出也的确是Apple Class啊··求指点 |