Class類中獲取方法:
public Method[] getMethods();//獲取包括自身和繼承(實現(xiàn))過來的所有的public方法——Method不支持泛型<>,即后面不接<>
public Method[] getDeclaredMethods();//獲取自身所有的方法(private、public、protected,和訪問權(quán)限無關(guān)),不包括繼承的
在jdk1.8后可以直接獲取私有屬性的方法不需要設(shè)置權(quán)限 但是僅限于getDeclaredMethod方法 對于Method 的方法仍然需要設(shè)置
權(quán)限。
public Method[] getMethod(String methodName, Class<T>...parameterTypes);//表示獲取指定的一個公共的方法,包括繼承的
參數(shù): methodName:表示獲取的方法的名字
parameterTypes:表示獲取的方法的參數(shù)的Class類型
public Method[] getDeclaredMethod(String methodName, Class<T>...parameterTypes);//表示獲取本類中的一個指定的方法(private、protected、public,與訪問權(quán)限無關(guān)),不包括繼承的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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[]{}中 。
1
2
3
|
@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)方法
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”); |
兩種方式調(diào)用靜態(tài)方法:
1. 因為靜態(tài)方法屬于所有實例對象公共的,可以創(chuàng)建該類的一個任意對象,通過該對象調(diào)用
staticMethod.invoke(clz.newInstance());//staticMethod無參,故參數(shù)列表類型不填
2. 如果底層方法是靜態(tài)的,那么可以忽略指定的obj參數(shù),將obj參數(shù)設(shè)置為null即可
staticMethod.invoke(null);
更多相關(guān)的內(nèi)容:
一:反射概念
可以通過Class類獲取某個類的成員變量以及方法,并且調(diào)用之。
二:通過反射獲取方法、變量、構(gòu)造方法
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 // 通過反射獲取類定義的構(gòu)造方法 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)用類定義的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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è)置參數(shù) 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