java反射獲取和調(diào)用方法
Class類中獲取方法:
public Method[] getMethods();//獲取包括自身和繼承(實現(xiàn))過來的所有的public方法——Method不支持泛型<>,即后面不接<>
public Method[] getDeclaredMethods();//獲取自身所有的方法(private、public、protected,和訪問權限無關),不包括繼承的
在jdk1.8后可以直接獲取私有屬性的方法不需要設置權限 但是僅限于getDeclaredMethod方法 對于Method 的方法仍然需要設置
權限。
public Method[] getMethod(String methodName, Class<T>...parameterTypes);//表示獲取指定的一個公共的方法,包括繼承的
參數(shù): methodName:表示獲取的方法的名字
parameterTypes:表示獲取的方法的參數(shù)的Class類型
public Method[] getDeclaredMethod(String methodName, Class<T>...parameterTypes);//表示獲取本類中的一個指定的方法(private、protected、public,與訪問權限無關),不包括繼承的方法
Class clazz = new Person().getClass(); try { //調(diào)用指定的方法 /*@SuppressWarnings("unchecked") Method me = clazz.getDeclaredMethod("getName", String.class); me.invoke(clazz.newInstance(),"zhangsan");*/ //獲取所有的方法 Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()); } } catch (Exception e) { e.printStackTrace(); }
對于多個參數(shù)的方法調(diào)用:getDeclaredMethod后面跟的parameterTypes可以理解為Class類型的形參 ,通過調(diào)用invoke來賦實參 多個參數(shù)的賦值 最好是包裝在new Object[]{}中 。
@SuppressWarnings("unchecked") Method method = clazz.getDeclaredMethod("getName",new Class[]{String.class,int.class}); method.invoke(new Person(), new Object[]{"zhangsan",10});
調(diào)用靜態(tài)方法
class User{ public static void staticMethod(){ System.out.println(“static mthod invoke.”); } } Eg: Class<User> clz=User.class; Method staticMethod=clz.getMethod(“staticMthod”);
兩種方式調(diào)用靜態(tài)方法:
1. 因為靜態(tài)方法屬于所有實例對象公共的,可以創(chuàng)建該類的一個任意對象,通過該對象調(diào)用
staticMethod.invoke(clz.newInstance());//staticMethod無參,故參數(shù)列表類型不填
2. 如果底層方法是靜態(tài)的,那么可以忽略指定的obj參數(shù),將obj參數(shù)設置為null即可
staticMethod.invoke(null);
更多相關的內(nèi)容:
一:反射概念
可以通過Class類獲取某個類的成員變量以及方法,并且調(diào)用之。
二:通過反射獲取方法、變量、構造方法
@Test // 通過反射獲取類定義的方法 public void testMethod() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); Method[] m = clazz.getDeclaredMethods(); for (int i = 0; i < m.length; i++) { System.out.println(m[i].getName() + " " + m[i].getDeclaringClass()); } } @Test // 通過反射獲取類定義的變量 public void testField() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); Field[] fields = clazz.getFields(); for (Field f : fields) { System.out.println(f.getName()); } } @Test // 通過反射獲取類定義的構造方法 public void testConstructor() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); @SuppressWarnings("rawtypes") Constructor[] cons = clazz.getConstructors(); for (@SuppressWarnings("rawtypes") Constructor c : cons) { System.out.println(c + " " + c.getDeclaringClass()); } }
三:通過反射調(diào)用類定義的方法
@Test // 通過反射調(diào)用類定義的方法 public void testInvokeMethod() throws Exception { Class clazz = Class.forName("java.lang.String"); // 定義參數(shù)類型 Class[] params = new Class[1]; params[0] = String.class; Method m = clazz.getDeclaredMethod("indexOf", params); // 設置參數(shù) Object[] p = new Object[1]; p[0] = "e"; Integer s = (Integer) m.invoke("helloworld!", p); System.out.println(s); }
相關文章
Mybatis中通過generator生成mapper、Dao、mapper.xml的方法
這篇文章主要介紹了Mybatis中通過generator生成mapper、Dao、mapper.xml的方法,需要的朋友可以參考下2017-01-01java查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼
下面小編就為大家?guī)硪黄猨ava查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06