复制构造函数不起作用

Copy constructor isn't working

我刚开始学习 java (OOP) 中的复制构造函数。 但出于某种原因,它对我不起作用。这就是我所做的: class 日期:

public class Date {
private int day;
private int month;
private int year;
//regular build function
public Date(int day,int month, int year)
{
  this.day=day;
  this.month=month;
  this.year=year;
}    
//copy constructor
public Date (Date date){
    this.day = date.day;
    this.year = date.year;
    this.month = date.month;
}   
public String toString()
{
  return this.day+"/"+this.month+"/"+this.year;
}
}

class护照:

 public class Passport {
private String name; //the name property
private int number; //the number property
private Date expiryDate; //the expiryDate property
//a regular building constructor
public Passport(String name,int number, Date expiryDate)
{
  this.name=name;
  this.number=number;
  Date copyExpiryDate = new Date(this.expiryDate);  
  this.expiryDate = copyExpiryDate;
} 
//a copy constructor
public Passport(Passport passport)
{
  this.name = passport.name;
  this.number = passport.number;
  this.expiryDate = passport.expiryDate;
}   

主要方法是:

 import java.util.*;
 public class Check {
static Scanner reader=new Scanner(System.in);
//checking if the classes are working properly
public static void main(String[] args){
    int numtraveler,passNum,passDay,passMonth,passYear,pay,planDay,planMonth,planYear;
    String name; 
    boolean isPay=false;
    System.out.println("how many travelers are you?");
    numtraveler=reader.nextInt();
    for(int i=0 ; i<numtraveler ; i++){
        System.out.println("enter your passport : ");
        System.out.println("enter name : ");
        name=reader.next();
        System.out.println("enter passport number : ");
        passNum=reader.nextInt();
        System.out.println("enter your passport's expiry date : (day then month then year) ");
        passDay=reader.nextInt();
        passMonth=reader.nextInt();
        passYear=reader.nextInt();
        Date d1=new Date(passDay,passMonth,passYear);
        System.out.println(d1);
        Passport p1=new Passport(name,passNum,d1);
        System.out.println(p1);
        Traveler t1=new Traveler(p1,isPay);
        System.out.println("do you want to pay? :(enter o if yes and 1 if not) ");
        pay=reader.nextInt();
        if(pay==0){
            t1.pay();
        }
        System.out.println("when are you planning to travel?(enter day then month then year)");
        planDay=reader.nextInt();
        planMonth=reader.nextInt();
        planYear=reader.nextInt();
        Date dPlan=new Date(planDay,planMonth,planYear);
        if(t1.checkTravel(dPlan)){
            System.out.println("The travel is possible");
        }
        else
            System.out.println("The travel isn't possible");            
    }
}
}

我从 class 中省略了一些不重要的信息 - get 和 set 日期,它们都有的一些其他功能以及另一个 class 称为 Traveler 的信息,因为它们与问题。 Traveler class 使用 Passport 复制构造函数,但它在那里有同样的问题。 当我 运行 main 方法到达日期时,创建对象 d1 并显示日期但是当它从 Passport class 创建对象 p1 时它 运行 一个错误: "Exception in thread "主要“java.lang.NullPointerException”。 但是,当我将 Passport class 更改为:

 Date copyExpiryDate = new Date(expiryDate.getDay(), 
        expiryDate.getMonth(), expiryDate.getYear());

有效。任何人都知道我在复制构造函数上做错了什么? 我非常感谢任何愿意提供帮助的人!

Passport 复制构造函数中,您没有创建到期日期的副本。

改变

public Passport(Passport passport)
{
  this.name = passport.name;
  this.number = passport.number;
  this.expiryDate = passport.expiryDate;
} 

public Passport(Passport passport)
{
  this.name = passport.name;
  this.number = passport.number;
  this.expiryDate = new Date(passport.expiryDate);
}  

另一个构造函数也有问题。

public Passport(String name,int number, Date expiryDate)
{
  this.name=name;
  this.number=number;
  Date copyExpiryDate = new Date(this.expiryDate);  
  this.expiryDate = copyExpiryDate;
}

应该是

public Passport(String name,int number, Date expiryDate)
{
  this.name=name;
  this.number=number;
  this.expiryDate = new Date(expiryDate);
} 

即您应该创建传递的 expiryDate 的副本,而不是 this.expiryDate.

的副本

这是由于

private Date expiryDate; 

expiryDate 实例变量被赋值为 null 并传递给

Date copyExpiryDate = new Date(this.expiryDate);

类似于 new Date(null);

这就是它抛出 NullPointerException 的原因。

我不认为这里的问题与副本有关constructor.I无法理解您使用变量 copyExpiryDate 的原因。我很确定这是导致您出现问题的行:

Date copyExpiryDate = new Date(this.expiryDate);

您的代码应如下所示:

public class Passport {
private String name; //the name property
private int number; //the number property
private Date expiryDate; //the expiryDate property

//a regular building constructor
public Passport(String name,int number, Date expiryDate)
{
  this.name=name;
  this.number=number;
  this.expiryDate = expiryDate;
} 
//a copy constructor
public Passport(Passport passport)
{
  this.name = passport.name;
  this.number = passport.number;
  this.expiryDate = passport.expiryDate;
}   

无论如何,如果您出于某种原因计划使用复制构造函数,该行应如下所示:

Date copyExpiryDate = new Date(expiryDate);

因为 this.expiry.date 会 return 无效。