我刚刚开始学习java(OOP)中的复制构造函数。 但由于某种原因,它对我不起作用。这就是我做的: 上课日期:
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;
}
}
护照类:
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");
}
}
}
我从类中省略了一些不重要的信息 - 从日期获取和设置,他们都有的其他一些函数和另一个名为Traveler的类,因为它们与问题无关。 Traveler类使用Passport拷贝构造函数,但它有同样的问题。 当我运行main方法时,它到达日期,创建对象d1并显示日期,但是当它从Passport类创建对象p1时,它会运行一个错误:“线程中的异常”main“java.lang.NullPointerException ”。 但是,当我将Passport类更改为:
时 Date copyExpiryDate = new Date(expiryDate.getDay(),
expiryDate.getMonth(), expiryDate.getYear());
它有效。谁知道我在复制构造函数中做错了什么? 我会非常感谢任何帮助过的人!
答案 0 :(得分:5)
在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
的副本。
答案 1 :(得分:2)
这是由于
发生的private Date expiryDate;
expiryDate实例变量将其赋值为null并在传递给
之后Date copyExpiryDate = new Date(this.expiryDate);
类似于新的Date(null);
这就是它抛出NullPointerException的原因。
答案 2 :(得分:1)
我认为这里的问题不在于复制构造函数。我无法理解你使用变量copyExpiryDate的原因。我很确定这是引起问题的因素:
日期copyExpiryDate =新日期(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;
}
无论如何,如果你打算出于某种原因使用拷贝构造函数,那么这行应该是这样的:
日期copyExpiryDate =新日期(expiryDate);
因为this.expiry.date
将返回null。