0

我一直在做一个基本的类继承练习,尽管我认为我已经掌握了它的要点,但我的程序并没有按照它应该的方式工作。我遇到编译错误并且无法弄清楚原因 - 如果你们都可以在这里帮助我,那就太好了。

所以开始,有三个文件 1. Person.java -base class 2. Student.java - Person.java 的派生类 3. Family.java -不太确定,我认为它是它自己的基类

Person.java 有两个实例变量,String name 和 int age,以及各种构造函数,toString、equals 和 set/get 方法

Student.java 同样是 Person 的派生类,根据定义,它将包含 Person 中包含的所有内容,以及另外两个实例 var,String major 和 double gpa。这个类也有major和gpa的get/set方法,一个比较一个班级学生和另一个班级学生的equals方法,我相信它被称为toString的一个覆盖方法,它在一个字符串中返回name、age、major和gpa .

最后,Family.java 是主要方法所在的位置。它创建一个 Person 类型的数组,将“Persons”添加到该数组中,然后输出它们。

我相当确定我的 Person.java 和 Student.java 都很好(我会将它们放在最底部以防万一),但我的问题在于 Family.java。我如何向数组中添加更多人(addPerson 方法)有问题,但我就是不知道是什么。


家庭.java

public class Family
{
private int famSize;
private Person[] family;

public Family(int size_of_family)
{
    famSize = size_of_family;
}   
public void addPerson(Person p)
{
    boolean isPresent = false;

    int i;
    for(i = 0; i < family.length; i++)
    {

        if(family[i].equals(p) == true)
        {
            isPresent = true;
            System.out.println(p.getName() + 
            " is already present in the family");
        }                       
    }       
    if(isPresent == false)
        family[i] = p;
}
public void printOutFamily()
{
    for(int i = 0; i < family.length; i++)
    {
        System.out.println(family[i].toString());
    }
}   
public static void main(String[] args)
{
   Person fred= new Person("Fred Flintstone", 50);
   System.out.println("created " + fred);

   Person wilma = new Person("Wilma Flintstone", 48);
   Student george= new Student("George Flintstone", 21, "Politics", 3.1);
   System.out.println("created " + george);

   Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3);
   Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4);
   Person yetAnotherGeorge= new Person("George Flintstone", 21);

   Family f = new Family(10);
   f.addPerson(fred);
   f.addPerson(wilma);
   f.addPerson(george);
   f.addPerson(sue);
   f.addPerson(anotherGeorge);
   f.addPerson(yetAnotherGeorge);

   anotherGeorge.setName("Georgie Flintstone");
   f.addPerson(anotherGeorge);

   f.printOutFamily();
}
}

人.java

public class Person 
{
private String name;
private int age;

public Person()
{
    name = "John Smith";
    age = 1;
}

public Person(String n, int a)
{
    name = n;
    age = a;
}

public String toString()
{
    return ("Name: " + getName() + ", Age: " + age + " ");
}

public boolean equals(Person otherPerson)
{
    return (getName().equals(otherPerson.getName()) && (age == otherPerson.age));
}

public String getName() 
{
    return name;
}

public void setName(String newName)
{
    name = newName;
}

public int getAge() 
{
    return age;
}

public void setAge(int newAge)
{
    age = newAge;
}
}

学生.java

public class Student extends Person
{
private String major;
private double gpa;

public Student()
{
    super();
    major = "Undecided";
    gpa = 0.0;
}

public Student(String theName, int theAge, String theMajor, double theGpa)
{
    super(theName, theAge);
    setMajor(theMajor);
    setGpa(theGpa);
}

public String toString()
{
    return ("Name: " + getName() + ", Age: " + getAge() + ", Major: " + major + ", GPA: " + gpa);
}

public boolean equals(Student otherStudent)
{
    return (major.equals(otherStudent.major) && (gpa == otherStudent.gpa));
}

public String getMajor() 
{
    return major;
}

public void setMajor(String newMajor)
{
    major = newMajor;
}

public double getGpa() 
{
    return gpa;
}

public void setGpa(double newGpa)
{
    gpa = newGpa;
}

}
4

2 回答 2

3

你应该添加

family = new Person[famSize];

在您的Family构造函数中初始化您的数组。它成为了:

public Family(int size_of_family)
{
    famSize = size_of_family;
    family = new Person[famSize];
}

正如@Pshemo 和@MarkM 指出的那样,您还需要在if语句中添加一个检查以确保family[i]它不为空:

for(i = 0; i < family.length; i++)
{

    if(family[i] != null && family[i].equals(p))
    {
        isPresent = true;
        System.out.println(p.getName() + 
        " is already present in the family");
    }                       
} 
于 2013-07-22T01:29:52.537 回答
0

问题出在这一行:

for(i = 0; i < family.length; i++)

此时,family 尚未初始化,将为 null。在调用 addPerson 方法之前,您有 3 个选项来初始化家庭:

1)在创建时初始化family

private Person[] family = new Person[5];

2)在构造函数中初始化family

private Person[] family = new Person[famSize];

3)在调用addPerson之前创建一个Family实例并初始化family属性(这样容易出错,应该避免)

于 2013-07-22T01:43:10.030 回答