java两个类很多属性一样,有没有什么快捷的方式把A类的属性值复制到B类,而不是通过一个一个set进去

java两个类很多属性一样,有没有什么快捷的方式把A类的属性值复制到B类,而不是通过一个一个set进去

我写的 工具类,可以参考一下:

import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * POJO工具类
 *
 * @author huazie
 * @version 2.0.0
 * @since 2.0.0
 */
public class POJOUtils {

    private POJOUtils() {
    }

    /**
     * 将源对象所有数据拷贝到目标对象中
     *
     * @param source 源对象
     * @param target 目标对象
     * @since 2.0.0
     */
    public static void copyAll(Object source, Object target) {
        if (ObjectUtils.isEmpty(source) || ObjectUtils.isEmpty(target)) return;
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 将源对象非空的数据拷贝到目标对象中
     *
     * @param source 源对象
     * @param target 目标对象
     * @since 2.0.0
     */
    public static void copyNotEmpty(Object source, Object target) {
        if (ObjectUtils.isEmpty(source) || ObjectUtils.isEmpty(target)) return;
        PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass());
        List<String> ignoreProperties = null;
        for (PropertyDescriptor sourcePd : sourcePds) {
            Method readMethod = sourcePd.getReadMethod();
            if (ObjectUtils.isEmpty(ReflectUtils.invoke(readMethod, source, null))) {
                if (ObjectUtils.isEmpty(ignoreProperties)) {
                    ignoreProperties = new ArrayList<>();
                }
                ignoreProperties.add(sourcePd.getName());
            }
        }
        if (ObjectUtils.isEmpty(ignoreProperties))
            BeanUtils.copyProperties(source, target);
        else
            BeanUtils.copyProperties(source, target, ignoreProperties.toArray(new String[0]));
    }

}

通过一个方法,传入需要复制属性的参数,然后再一个方法中,把需要复制参数的属性全部一次性赋值即可 ,但不知道是不是理解对了你的意思,测试代码如下:


p
public class B {

    int a;
    float b;
    double c;
    char d;
    String e;
    long f;
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 创建A对象
        A2 aObj = new A2(13,14.5f,15.6,'A',"张三");
        System.out.println(aObj); //打印A类对象aObj的属性值
        
        B bObj = new B(); // 创建无参的B对象
        bObj.setFiledsWithA(aObj); // 使用A类对象aObj的属性来给B对象相应成员变量赋值
        bObj.setF(16);  // 给B类对象bObj成员变量f赋值
        System.out.println(bObj); //打印B类对象bObj的属性值
    }
    
    // 通过一个方法,一次性把A2类对象aOjb属性的值复制到此B对象中
    public void setFiledsWithA(A2 aObj) {
        this.a = aObj.a;
        this.b = aObj.b;
        this.c = aObj.c;
        this.d = aObj.d;
        this.e = aObj.e;
    }
    
    public B() {
        
    }
    
    
    public B(int a, float b, double c, char d, String e,int f) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f= f;
    }
    
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public float getB() {
        return b;
    }
    public void setB(float b) {
        this.b = b;
    }
    public double getC() {
        return c;
    }
    public void setC(double c) {
        this.c = c;
    }
    public char getD() {
        return d;
    }
    public void setD(char d) {
        this.d = d;
    }
    public String getE() {
        return e;
    }
    public void setE(String e) {
        this.e = e;
    }

    public long getF() {
        return f;
    }

    public void setF(long f) {
        this.f = f;
    }

    @Override
    public String toString() {
        return "B [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", e=" + e + ", f=" + f + "]";
    }
    
    

}

class A2{
    
    int a;
    float b;
    double c;
    char d;
    String e;
    
    
    public A2() {
        
    }
    
    
    public A2(int a, float b, double c, char d, String e) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
    }
    
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public float getB() {
        return b;
    }
    public void setB(float b) {
        this.b = b;
    }
    public double getC() {
        return c;
    }
    public void setC(double c) {
        this.c = c;
    }
    public char getD() {
        return d;
    }
    public void setD(char d) {
        this.d = d;
    }
    public String getE() {
        return e;
    }
    public void setE(String e) {
        this.e = e;
    }


    @Override
    public String toString() {
        return "A2 [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", e=" + e + "]";
    }
    
    
    
}

img

单纯研究怎么快速赋值,那就是头疼医头,脚疼医脚
你可以自己写个工具类,利用反射赋值
但是你更应该反思,为什么完全无关的两个类,需要互相赋值?
难道它们之间不应该有继承关系吗