【业务背景】假设你是一家医院的开发人员需要编写一个Java程序来管理医生信息请编写一个Java程序实现以下功能:1 定义一个Doctor类包括医生姓名String name、医生编号int id、所属科室String department等属性;2 使用继承的方式创建一个ResidentDoctor类继承Doctor类表示住院医生增加一个病人列表属性ArrayListString patients
import java.util.ArrayList;
class Doctor {
String name;
int id;
String department;
public Doctor(String name, int id, String department) {
this.name = name;
this.id = id;
this.department = department;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Doctor doctor = (Doctor) o;
return id == doctor.id && name.equals(doctor.name) && department.equals(doctor.department);
}
@Override
public String toString() {
return "Doctor{" +
"name='" + name + '\'' +
", id=" + id +
", department='" + department + '\'' +
'}';
}
}
class ResidentDoctor extends Doctor {
ArrayList<String> patients;
public ResidentDoctor(String name, int id, String department) {
super(name, id, department);
patients = new ArrayList<>();
}
@Override
public String toString() {
return "ResidentDoctor{" +
"name='" + name + '\'' +
", id=" + id +
", department='" + department + '\'' +
", patients=" + patients +
'}';
}
}
public class DoctorManagement {
public static void main(String[] args) {
ArrayList<Doctor> doctors = new ArrayList<>();
Doctor doctor1 = new Doctor("张明", 1001, "外科");
Doctor doctor2 = new Doctor("李东", 1002, "内科");
ResidentDoctor residentDoctor1 = new ResidentDoctor("赵田", 2001, "眼科");
ResidentDoctor residentDoctor2 = new ResidentDoctor("王南", 2002, "骨科");
doctors.add(doctor1);
doctors.add(doctor2);
doctors.add(residentDoctor1);
doctors.add(residentDoctor2);
System.out.println("Is doctor1 equal to doctor2? " + doctor1.equals(doctor2));
System.out.println("Is doctor1 equal to residentDoctor1? " + doctor1.equals(residentDoctor1));
for (Doctor doctor : doctors) {
System.out.println(doctor);
}
int searchId = 1001;
String searchName = "张明";
for (Doctor doctor : doctors) {
if (doctor.id == searchId || doctor.name.equals(searchName)) {
System.out.println("找到医生: " + doctor);
}
}
}
}
``
原文地址: http://www.cveoy.top/t/topic/hXES 著作权归作者所有。请勿转载和采集!