【问题标题】:Do child/sub classes have to be in their own separate file in order to test? [duplicate]子类/子类是否必须在它们自己的单独文件中才能进行测试? [复制]
【发布时间】:2019-08-14 13:29:25
【问题描述】:

我正在使用 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;
}
}

【问题讨论】:

    标签: java eclipse class object hierarchy


    【解决方案1】:

    是的,每个文件应该有一个类。此外,您在Employee 类中使用MyDate 类,您需要扩展该类并且不能扩展多个类,因此最好使用java.util.Date 中存在的预定义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)
    

    有一些粗心的错误: 在员工类

    public static String ASSOCIATE_PROFESSOR + "associate professor";
    

    改为:

    public static String ASSOCIATE_PROFESSOR = "associate professor";
    

    在教师课堂上也是如此

    public static String ASSOCIATE_PROFESSOR + "associate professor";
    

    =代替+

    现在这段代码可以工作了。

    【讨论】:

    • 更正,每个文件应该有一个 public 类。一个文件可以有多个非公共类,但通常不鼓励这样做,因为这会使您的代码更难被发现。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 2021-06-20
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      相关资源
      最近更新 更多