Java中的invoke使用

來源:互聯網
上載者:User

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)的執行個體中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.