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

发布于 2024-12-11 09:49:18 字数 4021 浏览 0 评论 0原文

请不要生气,我很高兴这对我的作业有帮助,但我喜欢学习这个,但在任何地方都找不到。我已经寻找过它,但找不到类似的东西,我不知道如果我在所有错误的地方寻找错误的东西,我的代码中有另外两个类,并且在创建另一个包含类目标的类时遇到问题,我将展示我的尝试,看看你们是否可以帮助我。另外,我想确保我在其他两个课程中使用了关键字“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;    
      }
    }

请帮忙!感谢您的浏览

Please don't get mad, I am amiting this is help on my homework but I like to learn this and can't find it anywhere. I have looked for it and couldn't find anything like it, idk if I am looking in all the wrong places or for the wrong thing I have two other classes inside my code and having trouble creating another class with class objectives inside of it, I will show what I attempted and see if yall can please help me out. Also, I want to make sure I am using the keyword " this" right in my other two classes. She wanted us to use this in our program and the comments are what we are told to do:

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

And this is what I have been trying for my other class and It shows an ERROR:

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

Please help! And thank you for looking

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-12-18 09:49:19

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

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

Since you haven't specified the language or the error message, this is only a qualified guess. If this isn't the actual problem, provide more details.

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

You are declaring new local variables name and birthday here, rather than using the class members. That means the class members never get initialized, and I suspect that's what the error is trying to tell you.

The right way to do this is to just refer directly to the variables:

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

or, since this is implied when there is no local variable or parameter with the same name:

     name = new Person();
     birthday = new Date();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文