反射中Method类的invoke方法

就是调用类中的方法,最简单的用法是可以把方法参数化invoke(class, method);


Method Class.getMethod(String name, Class<?>... parameterTypes)的作用是获得对象所声明的公开方法

该方法的第一个参数name是要获得方法的名字,第二个参数parameterTypes是按声明顺序标识该方法形参类型。

person.getClass().getMethod("Speak", null);

//获得person对象的Speak方法,因为Speak方法没有形参,所以parameterTypes为null

person.getClass().getMethod("run", String.class);

//获得person对象的run方法,因为run方法的形参是String类型的,所以parameterTypes为String.class

如果对象内的方法的形参是int类型的,则parameterTypes是int.class


//getMethod第一个参数是方法名,第二个参数是该方法的参数类型,
//因为存在同方法名不同参数这种情况,所以只有同时指定方法名和参数类型才能唯一确定一个方法

Method method = XXX.getClass().getMethod(methodName,new Class[0]);

 //第一个参数是具体调用该方法的对象
 
//第二个参数是执行该方法的具体参数
    


如一个函数 int Test(int a, String str);

对应的getMethod方法:

1.  getMethod("Test",int.class,String.class);

2. getMethod("Test",new Class[]{int.class,String.class});

//Method类的invoke(Object obj,Object args[])方法接收的参数必须为对象,返回值总是对象。

 //如果参数为基本类型数据,必须转换为相应的包装类型的对象。

public Object invoke(Object obj,
                     Object... args)
              throws IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException

InvokeObj.java:

public class InvokeObj {
    public void show(){
        System.out.println("无参show()方法");
    }
    public void show(String name){
        System.out.println("show方法:"+name);
    }
    public String[] arrayShow(String[] arr){
        return arr;
    }
    public String stringShow(String str){
        return str;
    }
    public int intShow(int sum){
        return sum;
    }
}

MethodInvokeTest.java:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodInvokeTest {
    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        
        Class<InvokeObj> clazz=InvokeObj.class;
        Method[] methods=clazz.getMethods();
        //输出了Class类型的所有方法。
        System.out.println("以下输出InvokeObj类的方法");
        for(Method method:methods){
            System.out.println(method);
        }
        System.out.println();
        
         System.out.println("InvokeObj类的无参show()方法:");
         Method method1=clazz.getMethod("show",null);
        //会执行无参show()方法
         Object obj=method1.invoke(new InvokeObj(),null);
         System.out.print("输出无参show()方法的返回值:"+obj);
         System.out.println();System.out.println();
        
         System.out.println("InvokeObj类的show()方法: ");  
         Method method2 = clazz.getMethod("show", String.class);
         Object obj1 = method2.invoke(new InvokeObj(), "hello,world");  
           // System.out.println("输出有参show()方法: ");
         System.out.println();
        
         System.out.println("InvokeObj类的arrayShow()方法: ");  
         Method method3 = clazz.getMethod("arrayShow", String[].class);  
            String[] strs = new String[]{"hello", "world", "!"};  
            //数组类型的参数必须包含在new Object[]{}中,否则会报IllegalArgumentException  
            String[] strings = (String[]) method3.invoke(new InvokeObj(), new Object[]{strs});  
            for (String str : strings) {  
                System.out.println("arrayShow的数组元素:" + str);  
            }
         System.out.println();   
        
         System.out.println("InvokeObj类的StringShow()方法: ");  
            Method method4 = clazz.getMethod("stringShow", String.class);  
            String string = (String) method4.invoke(new InvokeObj(), "Thinking in java");  
            System.out.println("stringShow()方法的返回值: " + string);  
            System.out.println();
            
            System.out.println("InvokeObj类的intShow()方法: ");  
            Method method5 = clazz.getMethod("intShow", int.class);  
            int num = (Integer) method5.invoke(new InvokeObj(), 89);  
            System.out.println("intShow()方法的返回值: " + num);     
            
        
    }

}



猜你喜欢

转载自blog.csdn.net/weixin_40512519/article/details/80790445