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}----.执行可变参数的实例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值