java类的合并_对于两个实体类属性值的合并,java实现

时间:2025-02-16 11:58:17

packagetest;;public classCombineBeans {/*** 该方法是用于相同对象不同属性值的合并,如果两个相同对象中同一属性都有值,那么sourceBean中的值会覆盖tagetBean重点的值

*@paramsourceBean 被提取的对象bean

*@paramtargetBean 用于合并的对象bean

*@returntargetBean,合并后的对象*/

privateTestModel combineSydwCore(TestModel sourceBean,TestModel targetBean){

Class sourceBeanClass=();

Class targetBeanClass=();

Field[] sourceFields=();

Field[] targetFields=();for(int i=0; i

Field sourceField=sourceFields[i];

if((())){

continue;

}

Field targetField=targetFields[i];

if((())){

continue;

}

(true);

(true);try{if( !((sourceBean) == null) && !"serialVersionUID".equals(().toString())){

(targetBean,(sourceBean));

}

}catch (IllegalArgumentException |IllegalAccessException e) {

();

}

}returntargetBean;

}//测试 combineBeans方法

public static voidmain(String[] args) {

TestModel sourceModel= new TestModel(); //第一个对象

TestModel targetModel = new TestModel(); //第二个model对象

sourceModel.setProp1("1");

sourceModel.setProp2("1");

targetModel.setProp2("2");

targetModel.setProp3("2");

CombineBeans test= newCombineBeans();

(sourceModel, targetModel);

(());

}

}