浅谈反射 method方法
来源:互联网 发布:帝国时代地图编辑软件 编辑:程序博客网 时间:2024/05/12 06:41
package test;import java.lang.reflect.Method;public class TestM {public static void main(String[] args) { try{ TestM t = new TestM(); Class c = t.getClass(); Class[] cargs = new Class[2]; String[] realArgs = {"aa","bb"}; cargs[0] = realArgs.getClass(); Integer in = new Integer(2); cargs[1] = in.getClass(); Method m = c.getMethod("test",cargs); //调用了test方法,返回值为Method变量 Object[] inArgs = new Object[2]; inArgs[0] = realArgs; inArgs[1] = in; m.invoke(t,inArgs);//两个参数都是Object类型,第一个为当前类的对象,第二个为方法test参数的对象数组 }catch(Exception e){System.out.println(e);}}public void test(String[] str,Integer stri){ for(int j = 0; j < str.length; j ++) System.out.println(str[j]); System.out.println(stri.doubleValue());}}