java 复制一个类中的属性给另一个具有相同属性的类

直接上代码:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AttributeTransferUtil {
	
	private static final AttributeTransferUtil service = new AttributeTransferUtil();
	
	private final Logger LOG =LoggerFactory.getLogger(this.getClass());
	
	private AttributeTransferUtil(){}
	
	public static AttributeTransferUtil example(){
		return service;
	}
	
	public Object CloneAttribute(Object clone,Object beCloned){
		Field[] fieldClone = null;
		Field[] fieldBeCloned = null;
		Map map = new HashMap();
        try {
            Class classClone = clone.getClass();
            Class classBecloned = beCloned.getClass();

            fieldClone = classClone.getDeclaredFields();
            fieldBeCloned = classBecloned.getDeclaredFields();
            
            for(int t =0;t

A类中的某些属性在B类中存在,把相同的属性从A类复制到B类。约定优于配置,基于实体对象实现。

你可能感兴趣的:(Java)