Class類中獲取方法:
public Method[] getMethods();//獲取包括自身和繼承(實現)過來的所有的public方法——Method不支持泛型<>,即后面不接<>
public Method[] getDeclaredMethods();//獲取自身所有的方法(private、public、protected,和訪問權限無關),不包括繼承的
在jdk1.8后可以直接獲取私有屬性的方法不需要設置權限 但是僅限于getDeclaredMethod方法 對于Method 的方法仍然需要設置
權限。
public Method[] getMethod(String methodName, Class<T>...parameterTypes);//表示獲取指定的一個公共的方法,包括繼承的
參數: methodName:表示獲取的方法的名字
parameterTypes:表示獲取的方法的參數的Class類型
public Method[] getDeclaredMethod(String methodName, Class<T>...parameterTypes);//表示獲取本類中的一個指定的方法(private、protected、public,與訪問權限無關),不包括繼承的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Class clazz = new Person().getClass(); try { //調用指定的方法 /*@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(); } |
對于多個參數的方法調用:getDeclaredMethod后面跟的parameterTypes可以理解為Class類型的形參 ,通過調用invoke來賦實參 多個參數的賦值 最好是包裝在new Object[]{}中 。
1
2
3
|
@SuppressWarnings("unchecked") Method method = clazz.getDeclaredMethod("getName",new Class[]{String.class,int.class}); method.invoke(new Person(), new Object[]{"zhangsan",10}); |
調用靜態方法
1
2
3
4
5
6
7
8
|
class User{ public static void staticMethod(){ System.out.println(“static mthod invoke.”); } } Eg: Class< User > clz=User.class; Method staticMethod=clz.getMethod(“staticMthod”); |
兩種方式調用靜態方法:
1. 因為靜態方法屬于所有實例對象公共的,可以創建該類的一個任意對象,通過該對象調用
staticMethod.invoke(clz.newInstance());//staticMethod無參,故參數列表類型不填
2. 如果底層方法是靜態的,那么可以忽略指定的obj參數,將obj參數設置為null即可
staticMethod.invoke(null);
更多相關的內容:
一:反射概念
可以通過Class類獲取某個類的成員變量以及方法,并且調用之。
二:通過反射獲取方法、變量、構造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@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()); } } |
三:通過反射調用類定義的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Test // 通過反射調用類定義的方法 public void testInvokeMethod() throws Exception { Class clazz = Class.forName("java.lang.String"); // 定義參數類型 Class[] params = new Class[1]; params[0] = String.class; Method m = clazz.getDeclaredMethod("indexOf", params); // 設置參數 Object[] p = new Object[1]; p[0] = "e"; Integer s = (Integer) m.invoke("helloworld!", p); System.out.println(s); } |
原文鏈接:http://blog.csdn.net/sun_Hmtl/article/details/78703071