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)传入可变参数类型