import java.lang.reflect.Method;
*/
/*
* Method.invoke(对象,参数列表)
*/
class A {
public void add(int a, int b) {
System.out.println(a + b);
}
public void toUpper(String a) {
System.out.println(a.toUpperCase());
}
}
public class InvokeDemo {
public static void main(String[] args) {
A a = new A();
Class c = a.getClass();
try{
// Method method = c.getMethod("add", new Class[] {int.class, int.class});
Method method = c.getMethod("toUpper", new Class[] {String.class});
// 也可以用:Method method = c.getMethod("add", int.class, int.class);
method.invoke(a, "abc");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
简单Method.invoke
最新推荐文章于 2019-04-30 19:30:17 发布