简体   繁体   English

从obj2转换或创建obj1,其中obj1和obj2扩展相同的抽象类

[英]cast or create obj1 from obj2 where obj1 and obj2 extends same abstract class

For example I have: 例如,我有:

public abstract class SomeAbstract {
  private int a;
  private int b;
  ..
  private int z;
}

public class A extends SomeAbstract {
  private String aField;
}

public class B extends SomeAbstract {
  private long bField;
}

(default constructors/setters/getters are omitted) (省略了构造函数/ setter / getters的默认值)

I have some instance of a class A and i wanna create instance of a class B from A(abstract fields). 我有一个A类的实例,我想从A(抽象字段)创建一个B类的实例。

Yes, I can use abstract class constructor or create constructor for class B like this : 是的,我可以使用抽象类构造函数或为类B创建构造函数,如下所示:

public B(A a) {
  this.a = a.getA();
  this.b = a.getB();
  ..
  this.z = a.getZ(); 
}

But since I have many fields this does not look nice and convenient Is there another way? 但是因为我有很多字段,所以看起来不太方便又有另外一种方法吗?

You can create a constructor in the super class that receives another super class. 您可以在超类中创建一个接收另一个超类的构造函数。

public abstract class SomeAbstract {
    /* attributes... */

    public SomeAbstract() {
    }

    protected SomeAbstract(SomeAbstract another) {
        this.a = another.a;
        /* and on... */
    }

}

And reuse this constructor in sub classes: 并在子类中重用此构造函数:

public class B extends SomeAbstract {

    public B(A a) {
        super(a);
        this.specificAttribute = a.somethingElse;
    }

}

If you have that many fields and don't want/need to create the whole code manually, you can use an external library that helps you with the mapping between classes. 如果您有许多字段并且不希望/需要手动创建整个代码,则可以使用外部库来帮助您进行类之间的映射。 Some options are: 一些选项是:

  • Dozer that maps data between objects. 在对象之间映射数据的推土机 The configuration can be done through XML or annotations. 配置可以通过XML或注释完成。 This library works (AFAIK) using reflection, so it may have a direct impact on the performance of your application. 此库使用反射(AFAIK),因此它可能会直接影响应用程序的性能。
  • Orika that generates byte code at runtime to map data between objects. Orika在运行时生成字节代码以在对象之间映射数据。 The configuration is done at design time. 配置在设计时完成。 This library works (AFAIK) using reflection, so it may have a direct impact on the performance of your application, when I tested it, the execution time was faster than Dozer (about 7x faster). 这个库使用反射工作(AFAIK),因此它可能对应用程序的性能产生直接影响,当我测试它时,执行时间比Dozer快(大约快7倍)。
  • MapStruct that will generate code to map the classes (using getters and setters, validating nulls and on) and this code will be available for at runtime. 将生成代码以映射类的MapStruct (使用getter和setter,验证null和on),此代码将在运行时可用。 The configuration is done at design time through annotations. 配置在设计时通过注释完成。 This library works (AFAIK) using Java code, so it's similar to the normal execution of the code (eg execute b.setValue(a.getValue())) . 这个库使用Java代码工作(AFAIK),因此它类似于代码的正常执行(例如执行b.setValue(a.getValue()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换 Java 地图<Obj1, Collection<Obj2> &gt; 到地图<Obj2, Collection<Obj1> &gt; - Convert Java Map<Obj1, Collection<Obj2>> to Map<Obj2, Collection<Obj1>> obj1 == obj2实际比较什么 - what does obj1 == obj2 actually compare class obj =obj1; 是什么意思 - What is meaning of class obj =obj1; “(obj1.compareTo(obj2) &lt;= 0) ? one:two”是什么意思? - "(obj1.compareTo(obj2) <= 0) ? one:two" meaning? 如何将List <Obj1>转换为Map <Obj1.prop,List <Obj1.otherProp> - How to convert List<Obj1> to Map<Obj1.prop, List<Obj1.otherProp> 为什么java设计者强制要求如果obj1.equals(obj2)那么obj1.hashCode()必须是== obj2.hashCode() - Why did java designers impose a mandate that if obj1.equals(obj2) then obj1.hashCode() MUST Be == obj2.hashCode() 声明一个名为 obj1 的对象和名为 div(int x, int y) 的方法。 显示 obj1 将如何使用 div(int x, int y) 方法来计算 a/b - Declare an object called obj1 and method called div(int x, int y). Show how the obj1 will use the method div(int x, int y) to calculate a/b 为什么在外部使用Diamond运算符会失败 <String> .Inner obj2 = new Outer &lt;&gt;()。new Inner()? 虽然和Inner一样可以吗? - Why using diamond operator fails in Outer<String>.Inner obj2 = new Outer<>().new Inner() ? While same with Inner is OK? 如何替换“ return optObj.isPresent()”? 新的Obj2(optObj):null”是否处于功能样式? - How to replace “return optObj.isPresent() ? new Obj2(optObj) : null” in a functional style? Obj obj =new Obj(); 有什么区别? 和对象 obj; - What is the difference between Obj obj =new Obj (); and Obj obj;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM