package www.bittech;
import java.lang.reflect.Field;
import java.util.Date;
public class BeanCopy {
public static void copy(Object source,Object target){
//1、获取Class对象
Class sourceClass = source.getClass();
Class targetClass = target.getClass();
//2、获取属性对象
Field[] sourceFields = sourceClass.getDeclaredFields();
Field[] targetFields = targetClass.getDeclaredFields();
//3、属性赋值
//sourceFields = tatgetClass.getDeclaredFilelds
for(Field s:sourceFields){
for(Field t:targetFields){
//name,type
if(s.getName().equals(t.getName())&&s.getType()==t.getType()){
//赋值
//s -> value
//value -> t
try {
s.setAccessible(true);
Object value = s.get(source);
t.setAccessible(true);
t.s
将源对象中与目的对象相同的属性,源对象的属性值copy赋值给目的对象
最新推荐文章于 2021-09-13 18:03:50 发布
本文介绍了如何将源对象中与目的对象相同属性的值进行复制,实现属性值的赋值操作。通过示例代码详细阐述了这一过程,帮助读者理解对象属性间的值传递。
摘要由CSDN通过智能技术生成