问题
I've been working on a basic class inheritance exercise, and even though I think I've got the jist of it, my program isn't working the way it should. I'm getting compile errors and haven't been able to figure out why- it'd be great if you all could help me out here.
So to start off, there are three files 1. Person.java -base class 2. Student.java -a derived class of Person.java 3. Family.java -not quite sure, I think it's its own base class
Person.java has two instance variables, String name and int age, and an assortment of constructors, toString, equals, and set/get methods
Student.java, again, a derived class of Person, by definition will have all the stuff contained within Person, as well as two more instance vars, String major, and double gpa. This class also have get/set methods for major and gpa, an equals method that compares one class student with another class student, and I believe it's called an overidden method of toString that returns name, age, major, and gpa all in one string.
Lastly, Family.java is where the main method resides. It creates an array of type Person, adds "Persons" to this array, then outputs them.
I'm fairly certain my Person.java and my Student.java is fine (I will include them at the very bottom just in case), but my problem is with Family.java. There is something wrong with how I'm adding more people to the array (addPerson method), but I just can't figure out what.
Family.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();
}
}
Person.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;
}
}
Student.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;
}
}
回答1:
You should add
family = new Person[famSize];
in your Family constructor to initialize your array. It becomes:
public Family(int size_of_family)
{
famSize = size_of_family;
family = new Person[famSize];
}
As @Pshemo and @MarkM noted, you also need to add a check in your if statement to make sure family[i] isn't null:
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");
}
}
回答2:
The problem is at this line:
for(i = 0; i < family.length; i++)
at this point, family has not yet been initialized and it will be null. You have 3 options to initialize family before calling addPerson method:
1) initialize family at the time of creation
private Person[] family = new Person[5];
2) initialize family in the constructor
private Person[] family = new Person[famSize];
3) create an instance of Family and initialize family attribute before calling addPerson (this is error-prone and should be avoided)
来源:https://stackoverflow.com/questions/17778681/class-inheritance-in-java