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

时间:2011-10-23 01:28:51

标签: object constructor

请不要生气,我觉得这对我的作业有帮助,但我喜欢学习这个并且无法在任何地方找到它。我已经找了它,找不到类似的东西,idk如果我在错误的地方找错误或错误的话我在我的代码中有另外两个类,并且在创建另一个带有类目标的类时遇到了麻烦,我将展示我的尝试,看看你是否可以帮助我。另外,我想确保我在其他两个类中使用关键字“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;    
      }
    }

请帮忙!谢谢你的期待

1 个答案:

答案 0 :(得分:0)

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

public PersonalInfo()
{
     Person name = new Person();
     Date birthday = new Date();
     this.idNumber = 0; 
}

您在这里声明新的局部变量namebirthday,而不是使用类成员。这意味着类成员永远不会被初始化,我怀疑这是错误试图告诉你的。

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

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

或者,因为当没有本地变量或具有相同名称的参数时暗示this

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