Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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
Object 在为另一个类中的对象编写构造函数时遇到问题_Object_Constructor - Fatal编程技术网

Object 在为另一个类中的对象编写构造函数时遇到问题

Object 在为另一个类中的对象编写构造函数时遇到问题,object,constructor,Object,Constructor,请不要生气,我很抱歉这对我的家庭作业很有帮助,但我喜欢学习,在任何地方都找不到。我已经找过了,但找不到任何类似的东西,idk如果我找错了地方或找错了东西,我的代码中还有另外两个类,并且在创建另一个类时遇到了问题,其中包含了类目标,我将展示我的尝试,看看yall是否可以帮助我。另外,我想确保我在其他两个类中使用了关键字“this”。她希望我们在我们的节目中使用这一点,而这些评论是我们被告知要做的: public class Person { private String lastN

请不要生气,我很抱歉这对我的家庭作业很有帮助,但我喜欢学习,在任何地方都找不到。我已经找过了,但找不到任何类似的东西,idk如果我找错了地方或找错了东西,我的代码中还有另外两个类,并且在创建另一个类时遇到了问题,其中包含了类目标,我将展示我的尝试,看看yall是否可以帮助我。另外,我想确保我在其他两个类中使用了关键字“this”。她希望我们在我们的节目中使用这一点,而这些评论是我们被告知要做的:

public class Person
{
        private String lastName;
        private String firstName;

    //default constructor
public Person()
{
lastName= null;
firstName = null;
 }

    //two-parameter constructor 
 public Person(String lastName, String firstName)
{
this.lastName=lastName;
this.firstName=firstName;
}

    //copy constructor
public Person(Person object2)
{
this.lastName=object2.lastName;
 this.firstName=object2.firstName;
 }

    // standard accessor method for each of the two fields

public String getLastName(String lastName)
{
lastName = this.lastName;
 return lastName;
}

public String getFirstName(String firstName)
{
firstName = this.firstName;
return firstName;
}



public void setLastName(String lastName)
{
this.lastName=lastName;
}

public void setFirstName(String firstName)
{
 this.firstName=firstName;
}
        //mutator method for both fields—using standard mutator methods

public void setName(String lastName,String firstName)
{
this.lastName= lastName;
this.firstName = firstName;
}
    //toString method
public String toString()
{
 String str = “Last Name: “ + lastName + “\nFirst Name: “ + firstName;
 return str;
}


//equals method
public boolean equals(Person name2)
{
boolean status;
if (this.lastName.equals(name2.lastName) && this.firstName.equals(name2.firstName))
 status = true;
 else
 status = false;
 return status;
}

    //copy method
public Person copy()
 {
 Person copyObject = new Person(lastName, firstName);
 return copyObject;
 }
 }

public class Date
{    
        private int month;
        private int day;
        private int year;

    //default constructor
public Date()
{
this (0,0,0);
}


    //three-parameter constructor 
 public Date(int month, int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}


    //copy constructor

public Date(Date object2)
 {
this (object2.month, object2.day,  object2.year);
}


    //standard accessor method for each field
public int getMonth(int month)
{
month = this.month;
return month;
}
 public int getDay(int day)
{
day = this.day;
return day;
}

public int getYear(int year)
{
year = this.year;
return year;
}

    //standard mutator method for each field
 public void setMonth(int month)
 {
this.month = month;
 }
public void setDay(int day)
{
 this.day = day;
 }

 public void setYear(int year)
 {
 this.year = year;
 }

        //mutator method for both fields—using standard mutator methods

 public void setDate(int month, int day, int year)
 {
 this.month = month;
 this.day = day;
 this.year= year;
}


    //toString method

public String toString()
{
 String str = "Date:"  + month+ " " + day + ", " + year;
 return str;
 }


    //equals method
public boolean equals (Date object2)
 {
   boolean status;
   if (this.month == object2.month && this.day == object2.day && this.year ==         object2.year)
    status = true;
    else
    status = false;
    return status;
    }

    //copy method
    public Date copy()
    {
    Date copyObject = new Date(month, day, year);
     return copyObject;
     }
     }
这就是我为我的另一个类所做的尝试,它显示了一个错误:

   public class PersonalInfo
    {
            private Person name;
            private Date birthday;
            private int idNumber;
    // the default constructor      
        public PersonalInfo()
    {
         Person name = new Person();
         Date birthday = new Date();
         this.idNumber = 0; 
    }
    // A constructor that passes 6 parameters
      public PersonalInfo(String lastName, String firstName, int month, int day, int year, int idNumber )
      {
          Person name = new Person(lastName, firstName);
          Date birthday= new Date(month, day, year);
          this.idNumber = idNumber;    
      }
    }

请帮忙!感谢您查看

,因为您尚未指定语言或错误消息,这只是一个限定猜测。如果这不是实际问题,请提供更多详细信息

public PersonalInfo()
{
     Person name = new Person();
     Date birthday = new Date();
     this.idNumber = 0; 
}
您在此处声明新的局部变量
name
birth
,而不是使用类成员。这意味着类成员永远不会被初始化,我怀疑这就是错误试图告诉您的

正确的方法是直接引用变量:

     this.name = new Person();
     this.birthday = new Date();
或者,由于当没有同名的局部变量或参数时,会暗示此:

     name = new Person();
     birthday = new Date();

他谢谢,是的,我发现我不需要前面的类变量就可以这样做,但是我该如何制作一个通过6个参数的构造函数呢?和我一直做的一样?在有参数的一个上?@AlexAdamcik:构造函数的参数与方法的参数完全相同,所以是的,就是这样。另外,我在为这个类创建副本Cojnstructor时遇到了麻烦。我知道从public PersonalInfo(PersonalInfo object2)开始{但是我在调用此方法中的其他副本构造函数时遇到问题。有什么帮助吗?我会想“this.name=new Person(object2);”这会管用的,但是didnt@AlexAdamcik:复制构造函数采用的是
Person
,而不是
PersonalInfo
。必须使用要复制的实际
Person
对象,即
this.name=newperson(object2.name)
。非常感谢,我自己发现了这个,现在我可以澄清它是正确的。这很有意义。谢谢你的帮助。请你详细说明一下错误。顺便说一下,我认为你的getter是错误的。你只需要返回成员值,所以你不需要参数,你可以返回成员公共字符串getFirstName(){返回this.firstName;}