设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。
时间: 2024-03-15 10:43:50 浏览: 187
好的,以下是代码实现:
```
// MyDate类
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;
}
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
}
// 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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
", email='" + email + '\'' +
'}';
}
}
// 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + super.name + '\'' +
", status=" + status +
'}';
}
}
// Employee类
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;
}
@Override
public String toString() {
return "Employee{" +
"name='" + super.name + '\'' +
", office='" + office + '\'' +
", salary=" + salary +
", hireDate=" + hireDate +
'}';
}
}
// 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, MyDate hireDate, String officeHours, String rank) {
super(name, address, phoneNumber, email, office, salary, hireDate);
this.officeHours = officeHours;
this.rank = rank;
}
@Override
public String toString() {
return "Faculty{" +
"name='" + super.name + '\'' +
", office='" + super.office + '\'' +
", salary=" + super.salary +
", hireDate=" + super.hireDate +
", officeHours='" + officeHours + '\'' +
", rank='" + rank + '\'' +
'}';
}
}
// Staff类
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;
}
@Override
public String toString() {
return "Staff{" +
"name='" + super.name + '\'' +
", office='" + super.office + '\'' +
", salary=" + super.salary +
", hireDate=" + super.hireDate +
", title='" + title + '\'' +
'}';
}
}
// Test1类
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 < 10; i++) {
int type = random.nextInt(4) + 1; // 生成1-4的随机数,表示Person的子类类型
String name = "Person" + i;
String address = "Address" + i;
String phoneNumber = "123456789";
String email = "person" + i + "@test.com";
switch (type) {
case 1:
persons[i] = new Student(name, address, phoneNumber, email, random.nextInt(4) + 1);
break;
case 2:
persons[i] = new Faculty(name, address, phoneNumber, email, "Office" + i, random.nextDouble() * 10000, new MyDate(2021, 1, 1), "9:00-17:00", "Professor");
break;
case 3:
persons[i] = new Staff(name, address, phoneNumber, email, "Office" + i, random.nextDouble() * 10000, new MyDate(2021, 1, 1), "Manager");
break;
case 4:
persons[i] = new Employee(name, address, phoneNumber, email, "Office" + i, random.nextDouble() * 10000, new MyDate(2021, 1, 1));
break;
}
}
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person person : persons) {
System.out.println(person.toString());
}
}
}
```
在Test1类中,我们首先生成了一个长度为10的Person类型的数组,然后通过随机数来生成不同的Person子类对象,最后打印出每个对象的信息。
阅读全文