java 在不同的包中复制构造函数

java copy constructor in different package

我想在不同的包中创建一个复制构造函数。我该如何解决这个问题?这是我所拥有的。节日 Class 延长了活动 class。我知道我必须使用 super() 但我不知道在里面放什么。有人可以帮助我进一步了解吗?

public class Event {

private int year;
private int month;
private int numOfCities;

    public Event() {

       year = 0;
       month = 0;
       numOfCities = 0;

    }

public class Fair extends Event{

   private int numOfExib;
   private String exibType;

   public Fair() {

       super(0, 0, 0);
       numOfExib = 0;
       exibType = "";

   }

   public Fair(Fair anotherFair) {

       super(0,0,0); //I don't know what to put in here!
       numOfExib = anotherFair.numOfExib;
       exibType = anotherFair.exibType;

}

您应该为 Event 创建一个复制构造函数:

public class Event {
    private int year;
    private int month;
    private int numOfCities;

    /** default constructor */
    public Event() {
        year = month = numOfCities = 0; // unnecessary, as 0 is the default value
    }

    /** copy constructor */
    public Event(Event other) {
        year = other.year;
        month = other.month;
        numOfCities = other.numOfCities
    }
    ...
}

然后在子class:

public class Fair extends Event {
    private int numOfExib;
    private String exibType;

    /** default constructor */
    public Fair() {
        // no need to explicitly call default constructor
        numOfExib = 0; // unnecessary, but I like to explicitly initialize
        exibType = ""; // this is necessary to avoid a null value
    }

    /** copy constructor */
    public Fair(Fair other) {
        super(other); // parent class's copy constructor
        numOfExib = other.numOfExib;
        exibType = other.exibType;
    }
    ...
}

通过为 Event 创建复制构造函数,您可以将 Event 的内部与其子 class(es) 分离。 subclass 的复制构造函数只需要将要复制的对象传递给 Event,而 Event 的复制构造函数在内部处理自己的复制逻辑。

P.S.

我注意到您发布的代码调用了一个 Event 构造函数,该构造函数在您发布的代码中不存在——一个接受三个 int 参数(大概是年、月和城市数量事件)。如果您将该构造函数添加到基础 class,那么您可能希望将构造函数添加到 Fair class 以也传递显式值。由于 Fair class 还包含另外两个字段,因此您最终需要一个包含五个参数的构造函数。每当您开始看到像这样的长构造函数时,通常建议切换到构建器模式。 (网络搜索会在 Java 中找到很多关于此模式的教程。它很容易实现。)

构建器模式还解决了参数顺序的潜在混淆问题,当许多参数具有相同的数据类型时,这是一个特别危险的问题。例如,new Event(0, 0, 0) 是否按年、月和天数的顺序传递?还是月、年、日?或者是其他东西?使用构建器模式,您就不会出现这种模棱两可的情况。这个想法是以 class 结尾的,你会使用这样的东西:

Fair.Builder builder = new Fair.Builder();
builder.setYear(2018)
        .setMonth(7)
       // etc.
       .setExibType("recruitment");
Fair fair = builder.build();

为了实现这一点,Event 将有一个静态嵌套构建器 class(通常称为 Builder,但名称并不重要)并且 Fair 也会有扩展 Event.Builder.

的静态嵌套构建器 class

你几乎完成了,只需在父级中添加一个复制构造函数:

public class Event {
    private int year;
    private int month;
    private int numOfCities;

    public Event() {
      // by default all int fields are 0;
    }

    // This is a copy-constructor
    public Event(final Event otherEvent) {
       year = otherEvent.year;
       month = otherEvent.month;
       numOfCities = otherEvent.numOfCities;
    }
}

public class Fair extends Event {
   private int numOfExib;
   private String exibType;

   public Fair() {
       exibType = "";
   }

   public Fair(Fair anotherFair) {
       super(anotherFair); //Pass the given object to the super constructor

       numOfExib = anotherFair.numOfExib;
       exibType = anotherFair.exibType;
   }
}
 super(0, 0, 0);

这会引发错误。你的事件构造函数应该有这个:

public Event(int year, int month, int numberOfCities)

博览会也是。如果您想使用 super class 的构造函数,您的 child class 构造函数应该 包含与 相同数量的参数超级。虽然有这样的案例

public Fair(String name){
super(1, 3, 5);
// ^^These are "default values" saying that the event will always contain these values whenever you create a Fair instance
this.name = name;
}

上面的例子没有复制构造函数。复制构造函数就这么简单

public Fair(int year, int month, int numberOfCities){
super(year, month, numberOfCities);
}