import java.lang.reflect.*;
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] = readArgs; //呵呵,此處wingzhang大俠敲錯了應該是realArgs
inArgs[1] = in;
m.invoke(t,inArgs);
}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.intValue());
}
}
//
invoke(Object proxy, Method method, Object[] args)
在代理執行個體上處理方法調用並返回結果。
//
proxy - 在其上調用方法的代理執行個體
method - 對應於在代理執行個體上調用的介面方法的 Method 執行個體。Method 對象的聲明類將是在其中聲明方法的介面,該介面可以是代理類賴以繼承方法的代理介面的超介面。
args - 包含傳入代理執行個體上方法調用的參數值的對象數組,如果介面方法不使用參數,則為 null。基本類型的參數被封裝在適當基本封裝器類(如 java.lang.Integer 或 java.lang.Boolean)的執行個體中。