java 两个对象之间进行数据合并,实战分析

这篇博客分享了如何在Java中实现两个对象之间的数据合并,提供了针对任意对象和特定实体类的合并方法。此外,博主介绍了个人的开发背景和面试经验,还分享了一套全面的Java学习资料,包括面试题、学习笔记和实战项目,适用于不同阶段的开发者提升技能。
摘要由CSDN通过智能技术生成

*/

//针对所用对象

private static Object combineSydwCore(Object sourceBean, Object targetBean) {

Class sourceBeanClass = sourceBean.getClass();

Class targetBeanClass = targetBean.getClass();

Field[] sourceFields = sourceBeanClass.getDeclaredFields();

Field[] targetFields = sourceBeanClass.getDeclaredFields();

for (int i = 0; i < sourceFields.length; i++) {

Field sourceField = sourceFields[i];

Field targetField = targetFields[i];

sourceField.setAccessible(true);

targetField.setAccessible(true);

try {

if (!(sourceField.get(sourceBean) == null)) {

targetField.set(targetBean, sourceField.get(sourceBean));

}

} catch (IllegalArgumentException | IllegalAccessException e) {

e.printStackTrace();

}

}

return targetBean;

}

//针对单一对象,返回类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值