设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。
时间: 2024-03-15 16:45:25 浏览: 66
好的,根据您的需求,我为您提供以下代码实现:
Person.java
```java
public class Person {
private String name;
private String address;
private String phone;
private String email;
public Person(String name, String address, String phone, String email) {
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public String toString() {
return "Person: " + name;
}
}
```
Student.java
```java
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 phone, String email, int status) {
super(name, address, phone, email);
this.status = status;
}
public int getStatus() {
return status;
}
public String toString() {
return "Student: " + getName();
}
}
```
Employee.java
```java
public class Employee extends Person {
private String office;
private double salary;
private MyDate hireDate;
public Employee(String name, String address, String phone, String email, String office, double salary, MyDate hireDate) {
super(name, address, phone, 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;
}
public String toString() {
return "Employee: " + getName();
}
}
```
Faculty.java
```java
public class Faculty extends Employee {
private String officeHours;
private String rank;
public Faculty(String name, String address, String phone, String email, String office, double salary, MyDate hireDate, String officeHours, String rank) {
super(name, address, phone, email, office, salary, hireDate);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}
public String getRank() {
return rank;
}
public String toString() {
return "Faculty: " + getName();
}
}
```
Staff.java
```java
public class Staff extends Employee {
private String title;
public Staff(String name, String address, String phone, String email, String office, double salary, MyDate hireDate, String title) {
super(name, address, phone, email, office, salary, hireDate);
this.title = title;
}
public String getTitle() {
return title;
}
public String toString() {
return "Staff: " + getName();
}
}
```
MyDate.java
```java
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;
}
public String toString() {
return month + "/" + day + "/" + year;
}
}
```
Test1.java
```java
import java.util.Random;
public class Test1 {
public static void main(String[] args) {
Person[] persons = new Person[10];
Random rand = new Random();
for (int i = 0; i < persons.length; i++) {
int type = rand.nextInt(4);
switch (type) {
case 0:
persons[i] = new Person("Person" + i, "Address" + i, "Phone" + i, "Email" + i);
break;
case 1:
int status = rand.nextInt(4) + 1;
persons[i] = new Student("Student" + i, "Address" + i, "Phone" + i, "Email" + i, status);
break;
case 2:
String office = "Office" + i;
double salary = rand.nextDouble() * 10000;
int year = rand.nextInt(21) + 2000;
int month = rand.nextInt(12) + 1;
int day = rand.nextInt(28) + 1;
MyDate hireDate = new MyDate(year, month, day);
int facultyType = rand.nextInt(2);
if (facultyType == 0) {
String officeHours = "OfficeHours" + i;
String rank = "Rank" + i;
persons[i] = new Faculty("Faculty" + i, "Address" + i, "Phone" + i, "Email" + i, office, salary, hireDate, officeHours, rank);
} else {
String title = "Title" + i;
persons[i] = new Staff("Staff" + i, "Address" + i, "Phone" + i, "Email" + i, office, salary, hireDate, title);
}
break;
case 3:
break;
}
}
printMessages(persons);
}
public static void printMessages(Person[] persons) {
for (Person p : persons) {
if (p != null) {
System.out.println(p);
}
}
}
}
```
这份代码实现了您所需要的功能。Test1类中随机产生不同的对象,将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。每个类中的toString方法都被重写,以显示相应的类名和人名。
阅读全文