Method的invoke()方法的使用

public Object invoke(Object obj,
                     Object... args)
              throws IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException
对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。个别参数被自动解包,以便与基本形参相匹配,基本参数和引用参数都随需服从方法调用转换。

如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。

如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。 

如果底层方法是静态的,并且尚未初始化声明此方法的类,则会将其初始化。

如果方法正常完成,则将该方法返回的值返回给调用者;如果该值为基本类型,则首先适当地将其包装在对象中。但是,如果该值的类型为一组基本类型,则数组元素 被包装在对象中;换句话说,将返回基本类型的数组。如果底层方法返回类型为 void,则该调用返回 null。

obj - 从中调用底层方法的对象(简单的说就是调用谁的方法用谁的对象)
args - 用于方法调用的参数 

package test922;

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 num) {
        return num;
    }
}
package test922;

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

public class MethodInvokeTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<InvokeObj> clazz = InvokeObj.class;
        Method[] methods = clazz.getMethods();
        System.out.println("以下输出InvokeObj类的方法:");
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("InvokeObj类的无参show()方法:");
        Method method1 = clazz.getMethod("show", null);
        //会执行无参show()方法
        Object obj = method1.invoke(new InvokeObj(),null);
        System.out.println("输出无参show()方法的返回值:"+obj);
        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("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("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("InvokeObj类的intShow()方法: ");
        Method method5 = clazz.getMethod("intShow", int.class);
        int num = (int) method5.invoke(new InvokeObj(), 89);
        System.out.println("intShow()方法的返回值: " + num);
    }
}

以下输出InvokeObj类的方法:
public void test922.InvokeObj.show(java.lang.String)
public void test922.InvokeObj.show()
public java.lang.String[] test922.InvokeObj.arrayShow(java.lang.String[])
public java.lang.String test922.InvokeObj.StringShow(java.lang.String)
public int test922.InvokeObj.intShow(int)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
InvokeObj类的无参show()方法:
无参show()方法。
输出无参show()方法的返回值:null
InvokeObj类的show()方法: 
show方法:hello,world
输出有参show()方法: 
InvokeObj类的arrayShow()方法: 
arrayShow的数组元素:hello
arrayShow的数组元素:world
arrayShow的数组元素:!
InvokeObj类的StringShow()方法: 
StringShow()方法的返回值: Thinking in java
InvokeObj类的intShow()方法: 
intShow()方法的返回值: 89










  • 52
    点赞
  • 132
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Method中的invoke方法用于调用指定对象上的方法。它接受两个参数:第一个参数是要调用方法的对象实例,第二个参数是传递给方法的参数。通过调用invoke方法,可以动态地在运行时执行方法。\[1\] 在调用invoke方法之前,需要先获取到要调用的方法对象。可以使用Class中的getDeclaredMethod方法或getMethod方法来获取方法对象。getDeclaredMethod方法可以获取中所有的方法,包括public、protected、default和private方法,而getMethod方法只能获取public方法。这两个方法都需要指定方法的名称和参数型。\[3\] 在调用invoke方法之前,还需要确保方法对象是可访问的。如果方法是私有的或者位于父中,需要使用setAccessible方法方法设置为可访问。\[1\] 总结来说,要使用method.invoke方法,首先需要获取到要调用的方法对象,然后通过invoke方法传入对象实例和参数来执行方法。\[1\]\[3\] #### 引用[.reference_title] - *1* *2* [Methodinvoke方法初步了解](https://blog.csdn.net/zkkzpp258/article/details/106996081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [method.invoke()和invoke()简单理解](https://blog.csdn.net/u010886535/article/details/103955621)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值