利用java反射機制對方法進行調用

來源:互聯網
上載者:User

今天研究了一下java的反射機制,在此做個筆記。

 

基本步驟:首先擷取Class對象,然後用該對象擷取方法。具體的就看代碼吧。對照本文最後的結果,會更好的理解反射機制。

 

 

將要被反射的類:

package com.gzy.reflect; public class Reflect { private String name; private String psw; public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setPsw(String name){ this.psw=psw; } public String getPsw(){ return psw; } }

 

利用java機制的類

package com.gzy.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectUse { public boolean UseReflect(){ boolean flag=false; try { //>>>>>>>>>>>>>>>>>>>>>> //擷取某個類的Class對象有三種方式(其實就是擷取類Reflect),如下: //方法一:利用Class.forName(String param); Class cls=Class.forName("com.gzy.reflect.Reflect"); //方法二:使用 類名.class //Class cls=Reflect.class; //方法三:使用getClass() //Reflect r=new Reflect(); //Class cls=r.getClass(); //<<<<<<<<<<<<<<<<<<<<<<<< System.out.println("-------------cls is "+cls+"-------------------------"); //利用擷取到的類的Class對象建立一個執行個體(相當於Reflect new了個對象) Object obj=cls.newInstance(); System.out.println("-------------obj is "+obj+"-------------------------"); Class[] param=new Class[1]; param[0]=String.class; System.out.println("-------------param[0] is "+param[0]+"-------------------------"); //擷取Reflect的方法,第一個參數是方法名;第二個參數是參數的類型,注意是參數的類型。 Method med=cls.getMethod("setName",param); //null表示getName方法沒有參數 Method med1=cls.getMethod("getName", null); System.out.println("-------------med is "+med+"-------------------------"); // Object o=med.invoke(obj, new Object[]{"this is a reflect test."}); //開始調用方法,第一個參數是調用該方法的對象;第二個參數是值,即setName方法中要傳入的值 Object o=med.invoke(obj, "this is a reflect test."); System.out.println("-------------o is "+o+"-------------------------"); //方法沒有 參數的話就用null表示 Object o1=med1.invoke(obj, null); System.out.println("-------------o1 is "+o1+"-------------------------"); flag=true; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return flag; } }

 

測試類別:

 

package com.gzy.test; import com.gzy.reflect.ReflectUse; public class TestReflect { public static void main(String[] args){ boolean flag=false; ReflectUse rlu=new ReflectUse(); flag=rlu.UseReflect(); if(flag){ System.out.println("successful!"); }else{ System.out.println("fail~~~~"); } } }

 

測試結果:

-------------cls is class com.gzy.reflect.Reflect-------------------------
-------------obj is com.gzy.reflect.Reflect@d9f9c3-------------------------
-------------param[0] is class java.lang.String-------------------------
-------------med is public void com.gzy.reflect.Reflect.setName(java.lang.String)-------------------------
-------------o is null-------------------------
-------------o1 is this is a reflect test.-------------------------
successful!

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.