子类/子类是否必须在单独的文件中才能进行测试?

时间:2019-03-23 21:34:24

标签: java eclipse class object hierarchy

我正在使用Eclipse运行此代码程序来测试Person类及其子类。在Eclipse中,它显示出错误-每个子类必须在其自己的文件中定义。

我正在学习Java,并且想知道这是否必须吗?还是可以将它与父类和子类一起使用?如果我缺少任何东西,请指出正确的方向。谢谢!

这是我的代码:[我将其全部放在Eclipse的一个文件中]

import java.util.*;
//Test program to test Person class and its subclasses
public class Test {
public static void main(String[] args) {

    Person person = new Person("person");
    Student student = new Student ("student");
    Employee employee = new Employee("employee");
    Faculty faculty = new Faculty("faculty");
    Staff staff = new Staff("staff");

    //invoke toString() methods
    System.out.println(person.toString());
    System.out.println(student.toString());
    System.out.println(employee.toString());
    System.out.println(faculty.toString());
    System.out.println(staff.toString());
}
}

//Defining class Person
public class Person {
protected String name;
protected String address;
protected String phoneNum;
protected String email;

public Person(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress () {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPhoneNum() {
    return phoneNum;
}

public void setPhoneNum(String phoneNum) {
    this.phoneNum = phoneNum;
}

public String getEmail() {
    return email;
}

public void setEmail (String email) {
    this.email = email;
}

@Override
public String toString() {
    return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}

//Defines class Student extends Person
public class Student extends Person {
public static final String FRESHMAN = "freshman";
public static final String SOPHMORE = "sophmore";
public static final String JUNIOR = "junior";
public static final String SENIOR = "senior";

protected String classStatus;

public Student(String name) {
    super(name);
}

public Student(String name, String classStatus) {
    super(name);
    this.classStatus = classStatus;
}

@Override
public String toString() {
    return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}

//Defines class Employee extends Person
public class Employee extends Person {
protected double salary;
protected String office;
protected MyDate dateHired;

public Employee(String name) {
    this(name, 0, "none", new MyDate());
}

public Employee(String name, double salary, String office, MyDate dateHired) {
    super(name);
    this.salary = salary;
    this.office = office;
    this.dateHired - dateHired;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

public String getOffice() {
    return office;
}

public void setOffice (String office) {
    this.office = office;
}

public MyDate getDateHired() {
    return dateHired;
}

public void setDateHired(MyDate dateHired) {
    this.dateHired = dateHired;
}

@Override 
public String toString() {
    return "Name:"+getName()+"Class:" + this.getClass().getName();
}
}

//Defines class Faculty extends Employee
public class Faculty extends Employee {
public static String LECTURER = "lecturer";
public static String ASSISTANT_PROFESSOR = "assistant professor";
public static String ASSOCIATE_PROFESSOR + "associate professor";
public static PROFESSOR = "professor";

protected String officeHours;
protected String rank;

public Faculty(String name) {
    this(name, "9-5 PM", "Employee");
}

public Faculty(String name, String officeHours, String rank) {
    super(name);
    this.officeHours = officeHours;
    this.rank = rank;
}

public String getOfficeHours() {
    return officeHours;
}

public void setOfficeHours(String officeHours) {
    this.officeHours = officeHours;
}

public String getRank() {
    return rank;
}

public void setRank(String rank) {
    this.rank=rank;
}

@Override
public String toString() {
    return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}

//Defines class Staff extends Employee
public class Staff extends Employee {
protected String title;
public Staff(String name) {
    this(name, "none");
}

public Staff(String name, String title) {
    super(name);
    this.title=title;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Override
public String toString() {
    return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}

//Define class MyDate
public class MyDate {
private int month, day, year;

public MyDate (int month, int day, int year) {
    this.day=day;
    this.month=month;
    this.year=year;
}
}

2 个答案:

答案 0 :(得分:0)

是的,这是必须的。每个文件一个类。类可以具有内部类。您可以将子类定义为内部类。但是我建议将它们放在单独的文件中,并且不要使用内部类。

答案 1 :(得分:0)

是的,每个文件应该有一个类。此外,您正在使用MyDate类中的Employee类,您需要对其进行扩展,并且不能扩展多个类,因此最好使用现有的预定义Datejava.util.Date。将其导入Employee类中。

import java.util.Date;

代替此:

public Employee(String name, double salary, String office, MyDate dateHired)

使用:

public Employee(String name, double salary, String office, Date dateHired)

有一些粗心的错误: 在Employee类

public static String ASSOCIATE_PROFESSOR + "associate professor";

更改为:

public static String ASSOCIATE_PROFESSOR = "associate professor";

类似在教师班上

public static String ASSOCIATE_PROFESSOR + "associate professor";

放入=而不是+

现在此代码将可用。