设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。 版本2: 在上面实现类的基础上,为每个类增加一个将当前对象序列化到指定文件的方法writeToFile(File f)。为Student类创建一个新的子类Postgraduate,有研究方向和导师姓名两个新增数据域。编写测试类Test2,要求随机产生不同的对象,并将这些对象序列化到指定的文件中,并用方法printMessages(Person[] persons)将对象的信息输出。
时间: 2024-03-16 13:44:34 浏览: 50
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;
}
@Override
public String toString() {
return "Person: " + 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;
}
@Override
public String toString() {
return "Student: " + super.toString();
}
}
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: " + super.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;
}
@Override
public String toString() {
return "Faculty: " + super.toString();
}
}
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: " + super.toString();
}
}
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 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 rand = random.nextInt(4);
String name = "Person " + i;
String address = "Address " + i;
String phoneNumber = "Phone " + i;
String email = "Email " + i;
if (rand == 0) {
persons[i] = new Student(name, address, phoneNumber, email, Student.FRESHMAN);
} else if (rand == 1) {
persons[i] = new Faculty(name, address, phoneNumber, email, "Office " + i, 50000, new MyDate(2021, 1, 1), "9:00 - 17:00", "Professor");
} else if (rand == 2) {
persons[i] = new Staff(name, address, phoneNumber, email, "Office " + i, 40000, new MyDate(2021, 1, 1), "Staff " + i);
} else {
persons[i] = new Employee(name, address, phoneNumber, email, "Office " + i, 60000, new MyDate(2021, 1, 1));
}
}
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person person : persons) {
System.out.println(person);
}
}
}
```
以上是第一个问题的回答,如果您有任何疑问,可以随时问我。
阅读全文