设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。

时间: 2024-03-15 18:45:25 浏览: 8
好的,根据您的需求,我为您提供以下代码实现: 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方法都被重写,以显示相应的类名和人名。

相关推荐

最新推荐

recommend-type

基于matlab实现的数值计算及金融运用 ,金融时间序列数据分析 ,MATLAB和其他软件数据连接.rar

基于matlab实现的数值计算及金融运用 ,金融时间序列数据分析 ,MATLAB和其他软件数据连接.rar
recommend-type

使用SegNet进行语义分割-python源码.zip

使用SegNet进行语义分割-python源码.zip
recommend-type

JSP企业电子投票系统 2.zip

JSP企业电子投票系统 2
recommend-type

EmotionVGGnet情绪识别-python源码.zip

EmotionVGGnet情绪识别-python源码.zip
recommend-type

基于matlab实现的遗传算法、模拟退火算法、禁忌搜索算法求解VRP问题的matlab程序.rar

基于matlab实现的遗传算法、模拟退火算法、禁忌搜索算法求解VRP问题的matlab程序.rar
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

numpy数组索引与切片技巧

![numpy数组索引与切片技巧](https://img-blog.csdnimg.cn/f610d87ed50745d2b7052af887da2d0d.png) # 2.1 整数索引 整数索引是 NumPy 数组中索引元素的最简单方法。它允许您使用整数来访问数组中的特定元素或子数组。 ### 2.1.1 单个元素索引 单个元素索引使用一个整数来访问数组中的单个元素。语法为: ```python array[index] ``` 其中: * `array` 是要索引的 NumPy 数组。 * `index` 是要访问的元素的索引。 例如: ```python import
recommend-type

javaboolean类型怎么使用

Java中的boolean类型表示真或假,只有两个可能的值。在Java中,boolean类型的变量可以被初始化为false或true。可以使用以下语法来声明和初始化一个boolean类型的变量: ``` boolean myBoolean = true; ``` 在Java中,boolean类型的变量通常用于控制流程和条件测试,例如: ``` if (myBoolean) { // do something if myBoolean is true } else { // do something if myBoolean is false } ``` 除了if语句之外
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。