简体   繁体   English

Java类的继承

[英]Java Inheritance of Classes

I am trying to figure out Inheritance and Arrays in Java and I am trying to get these classes to work together. 我试图找出Java中的继承和数组,并且试图使这些类一起工作。 I believe I have the Inheritance down, but I am still struggling with the array part. 我相信我继承了下来,但是我仍然在努力与数组部分。

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 有三个文件:1. Person.java-基类2. Student.java-Person.java的派生类3. Family.java-不太确定,我认为这是它自己的基类

Person.java has two instance variables, String name and int age, and an assortment of constructors, toString, equals, and set/get methods Person.java有两个实例变量,字符串名称和整数年龄,以及各种构造函数,toString,equals和set / get方法

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. 再次,Student.java是Person的派生类,根据定义,它将包含Person中包含的所有内容,以及另外两个实例var,String major和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. 此类还具有用于major和gpa的get / set方法,这是一个将一个班级学生与另一个班级学生进行比较的equals方法,我相信这被称为toString的覆盖方法,该方法在一个字符串中返回名称,年龄,专业和gpa 。

Lastly, Family.java is where the main method resides. 最后,Family.java是主要方法所在的位置。 It creates an array of type Person, adds "Persons" to this array, then outputs them. 它创建一个类型为Person的数组,向该数组添加“ Persons”,然后输出它们。

I am getting an error that says: "main" java.lang.ArrayIndexOutOfBoundsException: 8 我收到一条错误消息:“ main” java.lang.ArrayIndexOutOfBoundsException:8

I can not figure out why this program is not working properly and would appreciate any help to figure this out. 我不知道为什么该程序无法正常工作,希望能帮助您解决此问题。 Thank you. 谢谢。

Person.java Class 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 Class 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;
}

}

Family.java Class Family.java类

public class Family
{
private int famArray = 0;
private Person[] family;

public Family(int size_of_family)
{
    famArray = size_of_family;
    family = new Person[famArray];
}   
public void addPerson(Person p)
{
    boolean isPresent = false;

    int 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");
        }                       
    }      
    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)
{
    Family f = new Family(8);
    Person fred= new Person("Fred Flintstone", 50);
    System.out.println("created " + fred);
    f.addPerson(fred);
    f.addPerson(fred);

    Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
    System.out.println("created "+ fredStudent);
    f.addPerson(fredStudent);

    Person wilma = new Person("Wilma Flintstone", 48);
    f.addPerson(wilma);


    Student george= new Student("George", 21, "Politics", 3.1);
    System.out.println("created " + george);
    f.addPerson(george);

    george.setName("Georgie");
    f.addPerson(new Student("George", 21, "Politics", 3.1));



    f.addPerson(new Student("John", 18, "Geology", 2.9));
    f.addPerson(new Student("Jane", 21, "Music", 3.2));
    f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));
    f.addPerson(new Student("Jim", 21, "Physics", 2.5));


    System.out.println("****** family listing: ");
    f.printOutFamily();

}
}

Here's the problem, in Family#addPerson method: 这是Family#addPerson方法中的问题:

if(isPresent == false)
    family[i] = p;

You're adding the element in position i . 您正在将元素添加到位置i If the element is not found, then i value will be family.length , thus giving you the exception. 如果未找到该元素,则i值将为family.length ,从而为您提供例外。

Use int famArray field instead: 使用int famArray字段代替:

if(isPresent == false) {
    family[famArray++] = p;
}

Or in an easier way for starters: 或以更简单的方式供初学者使用:

if(isPresent == false) {
    family[famArray] = p;
    famArray = famArray + 1;
}

As an addition to your current problem, you should first check if the famArray element is equals to family.length . 作为当前问题的补充,您应该首先检查famArray元素是否等于family.length If they're the same, then increase the array or do not allow more elements. 如果它们相同,则增加数组或不允许更多元素。


You state you have the same problem. 您说您有同样的问题。 This is because you're initializing famArray with the length of the array, noted in Family class constructor: 这是因为您正在使用Family类构造函数中指出的数组长度来初始化famArray

public Family(int size_of_family) {
    famArray = size_of_family; //here
    family = new Person[famArray];
}

Change the code to: 将代码更改为:

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

And you're done. 这样就完成了。

The problem is the line 问题是线

family[i] = p;

This occurs after a for loop which increments i to be equal to the length of the array. 这是在for循环之后发生的,该循环使i递增等于数组的长度。 I don't have any suggestions to fix it because I'm not sure what you are trying to do here. 我没有任何修复建议,因为我不确定您要在这里做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM