This article is continued from the above "Reflection to obtain class information" and uses reflection (invoke) to obtain methods in a class for execution.
1. Define a class that contains three methods with the same name and different parameters.
1 class A{ 2 public void print(){ 3 System.out.println("Hello,World"); 4 } 5 6 public void print(int a,int b){ 7 System.out.println(a+b); 8 } 9 public void print(String a,String b){10 System.out.println(a.toUpperCase()+","+b.toLowerCase());11 }12 }
2. Use the reflection (invoke) of the method to obtain three methods of the class respectively. Method and execution
1 public class Test { 2 public static void main(String[] args) { 3 /** 4 * 获取print(int,int)方法 5 * 1、获取类的类类型 6 */ 7 A a1 = new A(); 8 Class clazz = a1.getClass(); 9 /**10 * 2、获取方法(名称、参数列表)11 * getMethod()获取的是public方法12 * getDeclaredMethod()获取的是自己声明的方法13 */14 try {15 // Method m = clazz.getMethod("print",new Class[]{int.class,int.class});16 Method m = clazz.getMethod("print", int.class, int.class);17 //方法的反射操作,用m对象进行方法调用,a1.print完全相同18 //方法没有返回值返回null,有返回值返回具体的返回值19 // Object obj = m.invoke(a1,new Object[]{10,20});20 Object obj = m.invoke(a1,10,20);21 22 System.out.println("================================================");23 24 Method m2 = clazz.getMethod("print", String.class, String.class);25 m2.invoke(a1,"aaa","BBB");26 27 System.out.println("================================================");28 29 Method m3 = clazz.getMethod("print");30 m3.invoke(a1);31 } catch (Exception e) {32 e.printStackTrace();33 }34 }35 }
3, complete code
1 package com.format.test; 2 3 import java.lang.reflect.Method; 4 5 /** 6 * Created by Format on 2017/6/3. 7 */ 8 public class Test { 9 public static void main(String[] args) {10 /**11 * 获取print(int,int)方法12 * 1、获取类的类类型13 */14 A a1 = new A();15 Class clazz = a1.getClass();16 /**17 * 2、获取方法(名称、参数列表)18 * getMethod()获取的是public方法19 * getDeclaredMethod()获取的是自己声明的方法20 */21 try {22 // Method m = clazz.getMethod("print",new Class[]{int.class,int.class});23 Method m = clazz.getMethod("print", int.class, int.class);24 //方法的反射操作,用m对象进行方法调用,a1.print完全相同25 //方法没有返回值返回null,有返回值返回具体的返回值26 // Object obj = m.invoke(a1,new Object[]{10,20});27 Object obj = m.invoke(a1,10,20);28 29 System.out.println("================================================");30 31 Method m2 = clazz.getMethod("print", String.class, String.class);32 m2.invoke(a1,"aaa","BBB");33 34 System.out.println("================================================");35 36 Method m3 = clazz.getMethod("print");37 m3.invoke(a1);38 } catch (Exception e) {39 e.printStackTrace();40 }41 }42 }43 44 class A{45 public void print(){46 System.out.println("Hello,World");47 }48 49 public void print(int a,int b){50 System.out.println(a+b);51 }52 public void print(String a,String b){53 System.out.println(a.toUpperCase()+","+b.toLowerCase());54 }55 }
The above is the detailed content of Use reflection (invoke) to obtain methods in a class. For more information, please follow other related articles on the PHP Chinese website!