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());
}
}
浅谈反射 method方法
最新推荐文章于 2024-04-18 23:47:19 发布