Method类的Invoke方法

Dto:dto里面放的都是同一类型的字段

/*
 * Creation : 2 Dec 2015
 */
package com.java.invoke;

public class Dto {

    private Integer Col1;
    private Integer Col2;
    private Integer Col3;
    private Integer Col4;

    public Integer getCol1() {
        return Col1;
    }

    public void setCol1(Integer col1) {
        Col1 = col1;
    }

    public Integer getCol2() {
        return Col2;
    }

    public void setCol2(Integer col2) {
        Col2 = col2;
    }

    public Integer getCol3() {
        return Col3;
    }

    public void setCol3(Integer col3) {
        Col3 = col3;
    }

    public Integer getCol4() {
        return Col4;
    }

    public void setCol4(Integer col4) {
        Col4 = col4;
    }

}

下面要把list里面的数据逐一放入dto中

/*
 * Creation : 2 Dec 2015
 */
package com.java.invoke;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class TestInvoke {

    public static void main(String[] args) throws Exception {
        List<Integer> list = Arrays.asList(new Integer[] { 3, 5, 8 });

        Class<?> clazz = Class.forName(Dto.class.getName());

        int index = 1;

        Dto dto = new Dto();

        for (Integer value : list) {

            Method method = clazz.getDeclaredMethod("setCol" + index, Integer.class);

            method.invoke(dto, value);

            index++;

        }
        

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaxCode-1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值