今天研究了一下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!