admin 管理员组文章数量: 1086019
getMethod方法getMethod(String name, Class<?>... parameterTypes)传入可变参数类型
反射的getMethod方法getMethod(String name, Class<?>… parameterTypes)
Class<?>… parameterTypes 表示需要执行的方法Method的参数类型,及invoke里面传入的实例参数是那些,多个参数的话就传多个参数类型
对于传入可变参数,我们可以有两种传法,不做多余的讲述,看代码
public class FanShe {public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {List<String> l =new ArrayList<>();Map<String,String> h =new HashMap<>();Class<?> a = new TestFanShe().getClass();1: a.getMethod("say", Map.class,Map.class,Map.class).invoke(new TestFanShe(),h,h,h);2: a.getMethod("say", new Class<?>[] {Map.class,Map.class,Map.class}).invoke(new TestFanShe(),new Object[]{h,h,h});}
}class TestFanShe{public void say(Map<String,String> map,Map<String,String> map1,Map<String,String> map2){System.out.println("aaa");}
}
其实对于可变参数的时候我们都可以这么传,通过
new Class<?>[] {Map.class,Map.class,Map.class} -----》指定参数类型;
new Object[]{h,h,h}----.执行可变参数的实例
本文标签: getMethod方法getMethod(String name Class<> parameterTypes)传入可变参数类型
版权声明:本文标题:getMethod方法getMethod(String name, Class<?>... parameterTypes)传入可变参数类型 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1686559158a10216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论