前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >反射中Method类的invoke() 和getMethod()[通俗易懂]

反射中Method类的invoke() 和getMethod()[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-02 13:46:47
1.2K0
发布2022-08-02 13:46:47
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

就是调用类中的方法,最简单的用法是可以把方法参数化。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[])方法接收的参数必须为对象,返回值总是对象。

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

代码语言:javascript
复制
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); }

}

反射中Method类的invoke() 和getMethod()[通俗易懂]
反射中Method类的invoke() 和getMethod()[通俗易懂]

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125281.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档