设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。
时间: 2023-06-13 14:02:54 浏览: 335
以下是Person、Student、Employee、Faculty、Staff和MyDate类的实现代码:
```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().getSimpleName() + ": " + name;
}
}
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() + ", " + "Status: " + status;
}
}
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() + ", " + "Office: " + office + ", " + "Salary: " + salary + ", " + "Hire Date: " + hireDate.toString();
}
}
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() + ", " + "Office Hours: " + officeHours + ", " + "Rank: " + rank;
}
}
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() + ", " + "Title: " + title;
}
}
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;
}
}
```
以下是Test1类的实现代码:
```java
import java.util.Random;
public class Test1 {
public static void main(String[] args) {
Person[] persons = new Person[10];
Random random = new Random();
for (int i = 0; i < persons.length; i++) {
int type = random.nextInt(4);
switch (type) {
case 0:
persons[i] = new Person("Person" + i, "Address" + i, "PhoneNumber" + i, "Email" + i);
break;
case 1:
persons[i] = new Student("Student" + i, "Address" + i, "PhoneNumber" + i, "Email" + i, random.nextInt(4) + 1);
break;
case 2:
persons[i] = new Faculty("Faculty" + i, "Address" + i, "PhoneNumber" + i, "Email" + i, "Office" + i, random.nextDouble() * 10000, new MyDate(2020, random.nextInt(12) + 1, random.nextInt(28) + 1), "OfficeHours" + i, "Rank" + i);
break;
case 3:
persons[i] = new Staff("Staff" + i, "Address" + i, "PhoneNumber" + i, "Email" + i, "Office" + i, random.nextDouble() * 10000, new MyDate(2020, random.nextInt(12) + 1, random.nextInt(28) + 1), "Title" + i);
break;
}
}
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person person : persons) {
System.out.println(person.toString());
}
}
}
```
Test1类的主要功能是随机产生不同类型的Person、Student、Faculty和Staff对象,并将这些对象存储到数组中。然后,它使用printMessages方法将对象的信息输出。printMessages方法遍历persons数组,并调用每个对象的toString方法来输出对象的信息。由于每个类都重写了toString方法,因此输出的信息包含相应的类名和人名。
阅读全文