JAVA两个对象属性合并

时间:2025-03-26 17:39:15
import ;

public class CombineBeans {

	/**
	 * @Title: combineSydwCore
	 * @Description: 该方法是用于相同对象不同属性值的合并,如果两个相同对象中同一属性都有值,
	 *               那么sourceBean中的值会覆盖tagetBean重点的值
	 * @author: WangLongFei
	 * @date: 2017年12月26日 下午1:53:19
	 * @param sourceBean
	 *            被提取的对象bean
	 * @param targetBean
	 *            用于合并的对象bean
	 * @return targetBean 合并后的对象
	 * @return: Object
	 */
	@SuppressWarnings("unused")
	private Object combineSydwCore(Object sourceBean, Object targetBean) {
		Class sourceBeanClass = ();
		Class targetBeanClass = ();

		Field[] sourceFields = ();
		Field[] targetFields = ();
		for (int i = 0; i < ; i++) {
			Field sourceField = sourceFields[i];
			Field targetField = targetFields[i];
			(true);
			(true);
			try {
				if (!((sourceBean) == null)) {
					(targetBean, (sourceBean));
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				();
			}
		}
		return targetBean;
	}

	// 测试 combineBeans方法
	public static void main(String[] args) {
		Student sourceModel = new Student(); // 第一个对象
		Student targetModel = new Student(); // 第二个model对象

		("1");
		("张三");

		("2");
		("李四");
		("身份证");
		("222222222222222222222");

		CombineBeans test = new CombineBeans();
		(sourceModel, targetModel);
	}
}


public class Student {
	private String sex;
	private String cName;
	private String cardName;
	private String cardNumber;

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		 = sex;
	}

	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		 = cName;
	}

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		 = cardName;
	}

	public String getCardNumber() {
		return cardNumber;
	}

	public void setCardNumber(String cardNumber) {
		 = cardNumber;
	}

}