method.invoke运用

本文介绍了一个用于统计的数据对象模型——StatisticsDO,该模型利用反射机制实现了动态字段的更新与存储,能够灵活地处理不同类型的数据统计需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

package com.sitechasia.ebiz.publics.model;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

public class StatisticsDO {
	private Integer id;
	private int vpCount1;
	private int vpCount2;
	
	private transient Map<String, Integer> vpCount = new HashMap<String, Integer>();
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public int getVpCount1() {
		return vpCount1;
	}
	public void setVpCount1(int vpCount1) {
		this.vpCount1 = vpCount1;
		vpCount.put("vpCount1", vpCount1);
	}
	public int getVpCount2() {
		return vpCount2;
	}
	public void setVpCount2(int vpCount2) {
		this.vpCount2 = vpCount2;
		vpCount.put("vpCount2", vpCount2);
	}
	public int getVpCount(String type) {
		return vpCount.get(type);
	}
	public void setVpCount(String field, int value) {
		try {
			if (StringUtils.isNotBlank(field) && field.startsWith("vpCount")) {
				String methodName = "set" + field.replaceFirst("v", "V");
				Class<Integer> argType = int.class;
				Method method = this.getClass().getMethod(methodName, argType);
				method.invoke(this, (Object) value);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值