Java中的类继承 [英] Class Inheritance in Java

查看:158
本文介绍了Java中的类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个基本的类继承练习,即使我认为我有它的jist,我的程序不工作的方式它应该。我遇到编译错误,无法找出为什么 - 如果你都可以帮助我在这里很好。

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.

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

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有两个实例变量,String name和int age,以及一组
构造函数,toString,equals和set / get方法

Person.java has two instance variables, String name and int age, and an assortment of constructors, toString, equals, and set/get methods

Student.java,一个Person的派生类,根据定义,将包含Person中的所有东西,以及另外两个实例vars,String主,双gpa。
这个类还有主要和gpa的get / set方法,一个equals方法比较一个类学生和另一个类学生,我相信它被称为toString的覆盖方法,返回name,age,major和gpa所有在一个字符串。

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.

最后,Family.java是主要方法驻留的地方。它创建了一个Person类型的数组,向这个数组添加了Persons,然后输出它们。

Lastly, Family.java is where the main method resides. It creates an array of type Person, adds "Persons" to this array, then outputs them.

我相当肯定我的Person.java和Student.java罚款(我会包括他们在底部,以防万一),但我的问题是与Family.java。有一些问题,我如何添加更多的人数组(addPerson方法),但我只是不知道什么。

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.

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




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




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 = new Person [famSize];

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

in your Family constructor to initialize your array. It becomes:

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

如@Pshemo和@MarkM所述,您还需要添加一个您的 if 语句以确保 family [i] 不为空:

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");
    }                       
} 

这篇关于Java中的类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆