日期:2014-05-20 浏览次数:20808 次
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
class Person {
public Person(){
}
public Class[] function(Class[] args) {
System.out.println("public Class[] function(Class [] args)");
return null;
}
}
public class Demo {
public static void main(String[] args) throws Exception {
Test6();
}
// 我要反射Person的方法public Class[] function(Class [] args)
public static void Test6() throws Exception {
Class Person = Class.forName("Person");
Constructor con = Person.getConstructor();
Person p = (Person) con.newInstance();
Method m = Person.getMethod("function", Class[].class);
m.invoke(p, new Class[] {String.class,int.class});//这里为什么有错?
}
}
public static void Test6() throws Exception {
Class Person = Class.forName("com.huangjunwei.Person");
Constructor con = Person.getConstructor();
Person p = (Person) con.newInstance();
Method m = Person.getMethod("function", Class[].class);
Class[] clazzs = new Class[] {String.class,int.class};
m.invoke(p, new Object[]{clazzs});//这次就没错了
}