Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不同包中的java复制构造函数_Java_Inheritance_Package_Copy Constructor - Fatal编程技术网

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

不同包中的java复制构造函数,java,inheritance,package,copy-constructor,Java,Inheritance,Package,Copy Constructor,我想在另一个包中创建一个副本构造函数。我怎么能绕过这个?这是我的。Festival类扩展了Event类。我知道我必须使用super()但我不知道该放什么进去。有人能帮我进一步理解吗 public class Event { private int year; private int month; private int numOfCities; public Event() { year = 0; month = 0; numOfCitie

我想在另一个包中创建一个副本构造函数。我怎么能绕过这个?这是我的。Festival类扩展了Event类。我知道我必须使用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;

}

您应该为
事件
创建一个副本构造函数:

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
    }
    ...
}
然后在子类中:

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
的复制构造函数在内部处理自己的复制逻辑

附言

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

构建器模式还解决了关于参数顺序的潜在混淆问题,当许多参数具有相同的数据类型时,这是一种特殊的危险。例如,
新事件(0,0,0)
是否按该顺序传递年、月和天数?还是月、年、日?还是别的什么?使用构建器模式,就不会有这种歧义。这样做的目的是最终创建一个类,您可以使用这样的类:

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

为此,
Event
将有一个静态嵌套生成器类(通常称为
builder
,但名称并不重要),并且
Fair
还将有一个扩展
Event.builder的静态嵌套生成器类。您应该为
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
    }
    ...
}
然后在子类中:

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
的复制构造函数在内部处理自己的复制逻辑

附言

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

构建器模式还解决了关于参数顺序的潜在混淆问题,当许多参数具有相同的数据类型时,这是一种特殊的危险。例如,
新事件(0,0,0)
是否按该顺序传递年、月和天数?还是月、年、日?还是别的什么?使用构建器模式,就不会有这种歧义。这样做的目的是最终创建一个类,您可以使用这样的类:

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

为此,
Event
将有一个静态嵌套生成器类(通常称为
builder
,但名称并不重要),而
Fair
也将有一个扩展
Event.builder

的静态嵌套生成器类。如果您几乎完成了,只需在父级中添加一个副本构造函数即可:

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;
   }
}

您几乎完成了,只需在父级中添加一个副本构造函数:

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;
   }
}
这将抛出一个错误。事件构造函数应具有以下特性:

public Event(int year, int month, int numberOfCities)
集市也是。如果要使用
super
类的构造函数,则
子类的构造函数应包含与super相同数量的参数。虽然有这样的案例

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);
}
这将抛出一个错误。事件构造函数应具有以下特性:

public Event(int year, int month, int numberOfCities)
集市也是。如果要使用
super
类的构造函数,则
子类的构造函数应包含与super相同数量的参数。虽然有这样的案例

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);
}

您的
事件
构造函数不接受任何参数…我想在另一个包中创建一个副本构造函数该语句没有任何意义。构造函数属于类,因此不能在类之外声明。在另一个包中创建构造函数的想法与Java的设计方式背道而驰。回滚:请不要对问题的答案进行不完整计算您的
事件
构造函数不带任何参数…我想在另一个包中创建一个副本构造函数该语句没有任何意义。构造函数属于类,因此不能在类之外声明。在不同的包中创建构造函数的想法完全违背了Java的设计方式question@Mohanad-对我来说很好。您从编译器中得到了什么错误消息?您是否将它们放在了不同的包中?“我是被要求这样做的。”莫哈纳德-我没有把它们放在不同的pa中