设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。
时间: 2024-03-16 17:44:09 浏览: 82
好的,我理解了你的问题。下面是代码实现:
```
import java.util.Random;
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 String toString() {
return year + "-" + month + "-" + day;
}
}
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 toString() {
return "Person: " + name;
}
}
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 String toString() {
return "Student: " + super.toString();
}
}
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 toString() {
return "Employee: " + super.toString();
}
}
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 toString() {
return "Faculty: " + super.toString();
}
}
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 toString() {
return "Staff: " + super.toString();
}
}
public class Test1 {
public static void main(String[] args) {
Person[] persons = new Person[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
int type = random.nextInt(4);
switch (type) {
case 0:
persons[i] = new Student("Student" + i, "Address" + i, "Phone" + i, "Email" + i, random.nextInt(4) + 1);
break;
case 1:
persons[i] = new Faculty("Faculty" + i, "Address" + i, "Phone" + i, "Email" + i, "Office" + i, random.nextDouble() * 10000, new MyDate(2021, 1, 1), "Office Hours" + i, "Rank" + i);
break;
case 2:
persons[i] = new Staff("Staff" + i, "Address" + i, "Phone" + i, "Email" + i, "Office" + i, random.nextDouble() * 10000, new MyDate(2021, 1, 1), "Title" + i);
break;
default:
persons[i] = new Person("Person" + i, "Address" + i, "Phone" + i, "Email" + i);
}
}
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person person : persons) {
System.out.println(person.toString());
}
}
}
```
注意,这里只是一个简单的实现,如果需要更加完整的实现,可以考虑对每个类增加更多的属性和方法。
阅读全文