版本1: 设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出
时间: 2024-03-27 10:39:53 浏览: 62
Person类和它的两个子类Student、Employee
5星 · 资源好评率100%
好的,我理解了你的要求。这是一个比较复杂的类继承关系,需要仔细设计。以下是代码实现:
```java
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 getClass().getName() + ": " + getName();
}
}
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 super.toString() + " (Student)";
}
}
public class Employee extends Person {
private String office;
private double salary;
private MyDate hireDate;
public Employee(String name, String address, String phoneNumber, String email, String office, double salary, MyDate 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 MyDate getHireDate() {
return hireDate;
}
@Override
public String toString() {
return super.toString() + " (Employee)";
}
}
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, MyDate 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 super.toString() + " (Faculty)";
}
}
public class Staff extends Employee {
private String title;
public Staff(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String title) {
super(name, address, phoneNumber, email, office, salary, hireDate);
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return super.toString() + " (Staff)";
}
}
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
}
public class Test1 {
public static void main(String[] args) {
Person[] persons = new Person[10];
persons[0] = new Student("Alice", "123 Main St", "555-1234", "alice@example.com", Student.SENIOR);
persons[1] = new Student("Bob", "456 Oak St", "555-5678", "bob@example.com", Student.FRESHMAN);
persons[2] = new Employee("Charlie", "789 Maple St", "555-9012", "charlie@example.com", "A101", 50000, new MyDate(2020, 1, 1));
persons[3] = new Employee("David", "246 Elm St", "555-3456", "david@example.com", "B202", 60000, new MyDate(2019, 12, 31));
persons[4] = new Faculty("Emily", "135 Pine St", "555-7890", "emily@example.com", "C303", 70000, new MyDate(2018, 1, 1), "MWF 9-11", "Professor");
persons[5] = new Faculty("Frank", "579 Cedar St", "555-2345", "frank@example.com", "D404", 80000, new MyDate(2017, 12, 31), "TR 2-4", "Associate Professor");
persons[6] = new Staff("Grace", "802 Walnut St", "555-6789", "grace@example.com", "E505", 40000, new MyDate(2016, 1, 1), "Secretary");
persons[7] = new Staff("Henry", "246 Walnut St", "555-0123", "henry@example.com", "F606", 30000, new MyDate(2015, 12, 31), "Clerk");
persons[8] = new Student("Ivy", "358 Pine St", "555-4567", "ivy@example.com", Student.JUNIOR);
persons[9] = new Student("Jack", "913 Cedar St", "555-8901", "jack@example.com", Student.SOPHOMORE);
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person person : persons) {
System.out.println(person);
}
}
}
```
在测试类中随机产生了10个不同的Person对象,并将它们存储到数组中。然后调用printMessages方法,将每个对象的信息输出到控制台。每个对象都会调用其toString方法,输出类名和人名。
阅读全文