Java 有没有一种简单的方法可以将对象从类A解析为类B中的对象,即字段是类A的子集?

Java 有没有一种简单的方法可以将对象从类A解析为类B中的对象,即字段是类A的子集?,java,parsing,Java,Parsing,例如: class A{ String aa; String bb; String cc; String dd; } class B{ String aa; String bb; } 当然,我真正的类更大,有超过15个字段,这些字段本身也是复杂的对象。 现在我正在做什么 B parseToB(A a){ B b = new b(); b.setAa(a.getAa()); b.setBb(a.getBb());

例如:

class A{
    String aa;
    String bb;
    String cc;
    String dd;
}

class B{
    String aa;
    String bb;
}
当然,我真正的类更大,有超过15个字段,这些字段本身也是复杂的对象。 现在我正在做什么

B parseToB(A a){
    B b = new b();
    b.setAa(a.getAa());
    b.setBb(a.getBb());
    return b;
}

有没有一种简单的方法可以将A解析为B?库还是内置映射器类?在我的代码中,它看起来有点像一个奇怪的点,我刚刚设置了15行代码。

例如,您可以在
B
类中提供一个构造函数,该构造函数接受
a
作为参数:

class B {
    String aa;
    String bb;

    public B(A a) {
        this.aa = a.getAa();
        this.bb = a.getAa();
    }
 }
然后您可以从a开始创建一个新的B实例,只需调用构造函数:

B newObject = new B(a);
请记住,如果要创建不带参数的B类的新实例,还必须添加空构造函数(如果没有其他构造函数,则可以忽略此构造函数):


你可以使用反射。试试这个:

static class A {

        String aa, bb;
        Integer cc;
        Boolean dd;
    }

    static class B {

        String aa, bb;
        Integer cc;
        Integer dd;

        @Override
        public String toString() {
            return "B{" + "aa=" + aa + ", bb=" + bb + ", cc=" + cc + ", dd=" + dd + '}';
        }


    }

    public static void main(String []args) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        A a = new A();
        a.aa = "aa";
        a.bb = "bb";
        a.cc = 10;
        a.dd = true;
        B b = new B();
        for (Field declaredField : b.getClass().getDeclaredFields()) {
            if (a.getClass().getDeclaredField(declaredField.getName()) != null // both class have a field with the same name      
              && a.getClass().getDeclaredField(declaredField.getName()).getType()
                  .equals(declaredField.getType())) { // and the fields are of the same type
                declaredField.set(b, a.getClass().getDeclaredField(declaredField.getName()).get(a));
            }
        }
        System.out.println(b);

    }

我个人在我的项目中使用MapStruct来实现这类功能,它提供了一种非常灵活的方法来声明简单的映射,还为复杂的映射类型提供了很好的扩展

因此,对于您的示例,您可以简单地定义一个接口(或抽象类)

@Mapper
公共接口对象映射器{
ObjectMapper实例=Mappers.getMapper(ObjectMapper.class);
A mapToA(B);
B mapToB(A);
}
您需要在构建周期中配置注释处理器(如果您使用的是maven),然后只需在代码中调用映射器即可

A firstObject=newa()//设置变量
B secondObject=ObjectMapper.INSTANCE.mapToB(firstObject);

由于没有层次关系,因此无法解析这两者。仅仅因为这些变量具有相同的名称,并不意味着它们是相关的。不过,您可以映射它们。至少您应该提供一个副本构造函数或类似的构造函数,而不是一打公共setter。或用于自动搜索“对象到对象映射器”的解决方案。
static class A {

        String aa, bb;
        Integer cc;
        Boolean dd;
    }

    static class B {

        String aa, bb;
        Integer cc;
        Integer dd;

        @Override
        public String toString() {
            return "B{" + "aa=" + aa + ", bb=" + bb + ", cc=" + cc + ", dd=" + dd + '}';
        }


    }

    public static void main(String []args) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        A a = new A();
        a.aa = "aa";
        a.bb = "bb";
        a.cc = 10;
        a.dd = true;
        B b = new B();
        for (Field declaredField : b.getClass().getDeclaredFields()) {
            if (a.getClass().getDeclaredField(declaredField.getName()) != null // both class have a field with the same name      
              && a.getClass().getDeclaredField(declaredField.getName()).getType()
                  .equals(declaredField.getType())) { // and the fields are of the same type
                declaredField.set(b, a.getClass().getDeclaredField(declaredField.getName()).get(a));
            }
        }
        System.out.println(b);

    }