0

我正在做一个家庭作业问题,但不确定我的代码有什么问题。问题是: 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;
    }

当我编译时,错误提示“找不到符号 - 变量名”。谁能帮我解决这个问题。不知道为什么它找不到符号。

4

2 回答 2

1

您已经String partner;在 Person 类中定义了,但我想您的意思是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;
    }
于 2013-10-15T00:50:16.850 回答
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将引用 Person 中已经定义的 Person 伙伴

于 2013-10-15T01:09:22.317 回答