我正在做一个家庭作业问题,并且不确定我的代码有什么问题。问题是:http://prntscr.com/1xe4gd。
到目前为止我的代码:
public class Person //This is the class
{
String firstName;
String familyName;
boolean isFemale;
String partner;
}
到目前为止我的方法是:
Person getAngelinaJolie()
{
Person person1 = new Person();
person1.firstName = "Angelina";
person1.familyName = "Jolie";
person1.isFemale = false;
person1.partner.firstName = "Brad";
person1.partner.familyName = "Pitt";
return person1;
}
当我编译时,错误说“找不到符号 - 变量firstName”。有谁可以帮我这个。不知道为什么它找不到符号。
答案 0 :(得分:1)
您已在类Person中定义String partner;
,但我认为您的意思是Person partner;
,那么您可以在person1.partner = new Person();
之后设置它的属性
这是正确的代码:
Person getAngelinaJolie()
{
Person person1 = new Person();
person1.firstName = "Angelina";
person1.familyName = "Jolie";
person1.isFemale = true;
person1.partner = new Person();
person1.partner.firstName = "Brad";
person1.partner.familyName = "Pitt";
person1.partner.isFemale = false;
person1.partner.partner = person1;
return person1;
}
答案 1 :(得分:0)
public class Person //This is the class
{
String firstName;
String familyName;
boolean isFemale;
Person partner;
}
和现在的方法相同
Person getAngelinaJolie()
{
Person person1 = new Person();
person1.firstName = "Angelina";
person1.familyName = "Jolie";
person1.isFemale = false;
person1.partner.firstName = "Brad";
person1.partner.familyName = "Pitt";
return person1;
}
现在person.partner.firstname
将引用已经定义的人员合作伙伴