简体   繁体   English

Java 从基地创建 class

[英]Java creating devired class from base

Let's say I have a base class:假设我有一个基地 class:

Class A {
    public String field1;

    public A(String field1){
        this.field1 = field1;
    }
}

And a devired class:还有一个 class:

Class B extends A {
    public String field2;
}

Let's say I have a List<A> listA , how can I create a constructor so I can easily turn this list to List<B> listB ?假设我有一个List<A> listA ,我如何创建一个构造函数以便我可以轻松地将此列表转换为List<B> listB I want to have a constructor in B class that will take A object and fill in all the fields from A in B and have some logic to construct new fields.我想在B class中有一个构造函数,它将采用A object并填充BA的所有字段,并具有一些逻辑来构造新字段。 Something like this:像这样:

Class B extends A {
    public String field2;

    public B (A a){
        this = a;
        this.field2 = doLogic(a);
    }
}

Or at least just fill in all the fields from A in B:或者至少只填写 B 中 A 的所有字段:

public B(A a){
    this = a;
}

So later I can just manually set the field from B , to something like this:所以稍后我可以手动将B中的字段设置为如下所示:

listA.stream().map(x -> new B(x)).map(x -> x.setField2(logic(x)));

I know I can do it like this:我知道我可以这样做:

Class B extends A {
    public String field2;

    public B(A a){
        super(a.getField1());
    }
}

But this just doesn't look right to me, getting the fields of the object just to call a constructor of the same class.但这在我看来并不合适,获取 object 的字段只是为了调用同一个 class 的构造函数。

What if the base class has a lot of fields, will you need to get all of the fields to call in the constructor?如果base class 有很多字段怎么办,是否需要获取所有字段才能在构造函数中调用? It doesn't look good, so I'm wondering if is there any better way.看起来不太好,所以我想知道是否有更好的方法。

What if the base class has a lot of fields如果base class有很多字段怎么办

The first question of course would be why you need that and whether there might be another way.第一个问题当然是您为什么需要它以及是否有其他方法。 In this answer I'll assume you've already answered that question and came to the conclusion you need it.在这个答案中,我假设您已经回答了这个问题并得出了您需要它的结论。

Let's say I have a List<A> listA , how can I create a constructor so I can easily turn this list to List<B> listB ?假设我有一个List<A> listA ,我如何创建一个构造函数以便我可以轻松地将此列表转换为List<B> listB

You could do a copy constructor and maybe also do something like this:你可以做一个复制构造函数,也可以做这样的事情:

class A { 
  A(A other) {
    //copy fields
  }
}

class B extends A {
  B(A other) {
    super(other);
    //init B fields
  }
}

With this in place you can use plain iteration or streaming to turn a List<A> into a List<B> :有了这个,您可以使用普通迭代或流式处理将List<A>转换为List<B>

List<B> convertedList = origList.stream().map(a -> new B(a)).collect(Collectors.toList());

Another option might be delegation but that would depend on your classes:另一种选择可能是委托,但这取决于您的课程:

class B extends A {
  A delegate;      

  B(A delegate) {
    this.delegate = delegate;
    //init B fields
  }

  @Override
  void setField1(String value) {
    delegate.setField1(value);
  }

  @Override
  String getField1() {
    return delegate.getField1();
  }
}

Doing it like this would leave B's "field1" empty so it might not be the best option.这样做会使 B 的“field1”为空,因此它可能不是最佳选择。 If you have an interface for A it would look similar but leave less "empty" fields:如果你有一个 A 的接口,它看起来很相似但留下更少的“空”字段:

interface AInterface {
  void setField1(String value);
  String getField1();
}

class A implements AInterface {
   ...
}

class B implements AInterface {
  AInterface delegate;
  
  B(AInterface delegate) {
     this.delegate = delegate;
    //init B fields
  }

  @Override //this is not an actual override but an implementation
  public void setField1(String value) {
    delegate.setField1(value);
  }

  @Override //this is not an actual override but an implementation
  public String getField1() {
    return delegate.getField1();
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM