简单Method.invoke

来源:互联网 发布:淘宝全屏热点代码 编辑:程序博客网 时间:2024/06/10 03:51
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();}}}

0 0
原创粉丝点击