设计一个名为Person的类和它的两个名为Student 和 Employee的子类。Employee类又有子类:教员类Faculty 和职员类Staff。每个人都有姓名、地址、电话号码和电子邮件地址。学生有班级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员涉及办公室、工资教员有办公时间和级别。职员有职务称号。覆盖每个类中的toString方法,显示相应的类别名字和人名。实现这些类,并编写一个测试程序,创建Person、Student、Employee、Faculty和 Staff,并且调用它们的toString ()方法。
时间: 2024-03-22 20:41:28 浏览: 228
下面是一个可能的实现,包含Person、Student、Employee、Faculty和Staff类,以及一个测试程序:
```java
// Person类
public class Person {
private String name;
private String address;
private String phoneNumber;
private String email;
public Person(String name, String address, String phoneNumber, String email) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "Person: " + name;
}
}
// Student类
public class Student extends Person {
public static final int FRESHMAN = 1;
public static final int SOPHOMORE = 2;
public static final int JUNIOR = 3;
public static final int SENIOR = 4;
private int status;
public Student(String name, String address, String phoneNumber, String email, int status) {
super(name, address, phoneNumber, email);
this.status = status;
}
public int getStatus() {
return status;
}
@Override
public String toString() {
return "Student: " + getName();
}
}
// Employee类
public class Employee extends Person {
private String office;
private double salary;
private String hireDate;
public Employee(String name, String address, String phoneNumber, String email, String office, double salary, String hireDate) {
super(name, address, phoneNumber, email);
this.office = office;
this.salary = salary;
this.hireDate = hireDate;
}
public String getOffice() {
return office;
}
public double getSalary() {
return salary;
}
public String getHireDate() {
return hireDate;
}
@Override
public String toString() {
return "Employee: " + getName();
}
}
// Faculty类
public class Faculty extends Employee {
private String officeHours;
private String rank;
public Faculty(String name, String address, String phoneNumber, String email, String office, double salary, String hireDate, String officeHours, String rank) {
super(name, address, phoneNumber, email, office, salary, hireDate);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}
public String getRank() {
return rank;
}
@Override
public String toString() {
return "Faculty: " + getName();
}
}
// Staff类
public class Staff extends Employee {
private String title;
public Staff(String name, String address, String phoneNumber, String email, String office, double salary, String hireDate, String title) {
super(name, address, phoneNumber, email, office, salary, hireDate);
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Staff: " + getName();
}
}
// 测试程序
public class Test {
public static void main(String[] args) {
Person person = new Person("John", "123 Main St", "555-1234", "john@example.com");
System.out.println(person);
Student student = new Student("Jane", "456 Oak St", "555-5678", "jane@example.com", Student.SOPHOMORE);
System.out.println(student);
Employee employee = new Employee("Bob", "789 Elm St", "555-9012", "bob@example.com", "A101", 50000, "1/1/2021");
System.out.println(employee);
Faculty faculty = new Faculty("Alice", "345 Pine St", "555-3456", "alice@example.com", "B202", 80000, "1/1/2020", "MWF 9-11", "Professor");
System.out.println(faculty);
Staff staff = new Staff("Tom", "678 Maple St", "555-7890", "tom@example.com", "C303", 40000, "1/1/2019", "Clerk");
System.out.println(staff);
}
}
```
输出结果:
```
Person: John
Student: Jane
Employee: Bob
Faculty: Alice
Staff: Tom
```
阅读全文