00113_通過反射獲取成員方法並使用
阿新 • • 發佈:2018-01-01
tor 執行指定 eth 演示 反射 println span 通過 font
1、在反射機制中,把類中的成員方法使用類Method表示;
2、通過Class類中提供的方法獲取成員方法:
(1)返回獲取一個方法
public Method getMethod(String name, Class<?>... parameterTypes) 獲取public 修飾的方法 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 獲取任意的方法,包含私有的 參數1: name 要查找的方法名稱; 參數2: parameterTypes 該方法的參數類型
(2)返回獲取多個方法
public Method[] getMethods() 獲取本類與父類中所有public 修飾的方法 public Method[] getDeclaredMethods() 獲取本類中所有的方法(包含私有的
(3)代碼演示
1 package cn.gzdlh_01_Reflect; 2 3 import java.lang.reflect.Method; 4 5 public class MethodDemo { 6 public static void main(String[] args) throws ClassNotFoundException,7 NoSuchMethodException, SecurityException { 8 // 獲取Class對象 9 Class c = Class.forName("cn.gzdlh_01_Reflect.Person"); 10 11 // 獲取多個方法 12 // Method[] methods = c.getMethods(); 13 Method[] methods = c.getDeclaredMethods(); 14 for (Method method : methods) {15 System.out.println(method); 16 } 17 18 System.out.println("-----------------------"); 19 // 獲取一個方法: 20 // public void method1() 21 Method method = c.getMethod("method1", null); 22 System.out.println(method); 23 // public String method4(String name){ 24 method = c.getMethod("method4", String.class); 25 System.out.println(method); 26 // 私有方法 27 // private void method5() 28 method = c.getDeclaredMethod("method5", null); 29 System.out.println(method); 30 } 31 }
3、通過反射,創建對象,調用指定的方法
(1)獲取成員方法,步驟如下:
①獲取Class對象;
② 獲取構造方法;
③通過構造方法,創建對象;
④獲取指定的方法;
⑤執行找到的方法。
public Object invoke(Object obj, Object... args) 執行指定對象obj中,當前Method對象所代表的方法,方法要傳入的參數通過args指定
(2)代碼演示
1 package cn.gzdlh_01_Reflect; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 7 public class MethodDemo2 { 8 public static void main(String[] args) throws ClassNotFoundException, 9 NoSuchMethodException, SecurityException, InstantiationException, 10 IllegalAccessException, IllegalArgumentException, 11 InvocationTargetException { 12 // 1, 獲取Class對象 13 Class c = Class.forName("cn.gzdlh_01_Reflect.Person"); 14 // 2,獲取構造方法 15 // public Person(String name, int age, String address){ 16 Constructor con = c.getConstructor(String.class, int.class, 17 String.class); 18 // 3,通過構造方法,創建對象 19 Object obj = con.newInstance("gzdlh", 23, "廣州"); 20 // 4,獲取指定的方法 21 // public void method1() 沒有返回值沒有參數的方法 22 // Method m1 = c.getMethod("method1", null); 23 24 // public String method4(String name) 25 Method m4 = c.getMethod("method4", String.class); 26 27 // 5,執行找到的方法 28 // m1.invoke(obj, null); 29 30 Object result = m4.invoke(obj, "gzdlh"); 31 System.out.println("result = " + result); 32 } 33 }
4、通過反射,創建對象,調用指定的private 方法
(1)獲取私有成員方法,步驟如下:
①獲取Class對象;
②獲取構造方法;
③通過構造方法,創建對象;
④獲取指定的方法;
⑤開啟暴力訪問;
⑥執行找到的方法。
public Object invoke(Object obj, Object... args) 執行指定對象obj中,當前Method對象所代表的方法,方法要傳入的參數通過args指定。
(2)代碼演示
1 package cn.gzdlh_01_Reflect; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 7 public class MethodDemo3 { 8 public static void main(String[] args) throws ClassNotFoundException, 9 NoSuchMethodException, SecurityException, InstantiationException, 10 IllegalAccessException, IllegalArgumentException, 11 InvocationTargetException { 12 // 1, 獲取Class對象 13 Class c = Class.forName("cn.gzdlh_01_Reflect.Person"); 14 // 2,獲取構造方法 15 // public Person(String name, int age, String address){ 16 Constructor con = c.getConstructor(String.class, int.class, 17 String.class); 18 // 3,通過構造方法,創建對象 19 Object obj = con.newInstance("gzdlh", 23, "廣州"); 20 // 4,獲取指定的方法 21 // private void method5(){ 22 Method m5 = c.getDeclaredMethod("method5", null); 23 // 5,開啟暴力訪問 24 m5.setAccessible(true); 25 // 6,執行找到的方法 26 m5.invoke(obj, null); 27 } 28 }
在反射機制中,把類中的成員方法使用類Method表示
00113_通過反射獲取成員方法並使用