import java.util.ArrayList; import java.util.List; import java.util.Random; public class HospitalMonitor { private List<Patient> patients = new ArrayList<Patient>(); private List<Staff> staffs = new ArrayList<Staff>(); // 添加患者 public void addPatient(Patient patient) { patients.add(patient); } // 添加医护人员 public void addStaff(Staff staff) { staffs.add(staff); } // 监测病房 public void monitorWard() { Random random = new Random(); for (Patient patient : patients) { // 模拟患者情况,50%概率出现异常 if (random.nextDouble() < 0.5) { // 触发警报 Alert alert = new Alert(patient); // 通知医护人员 for (Staff staff : staffs) { staff.receiveAlert(alert); } } } } } // 患者类 class Patient { private String name; public Patient(String name) { this.name = name; } public String getName() { return name; } } // 警报类 class Alert { private Patient patient; public Alert(Patient patient) { this.patient = patient; } public Patient getPatient() { return patient; } } // 医护人员类 abstract class Staff { private String name; public Staff(String name) { this.name = name; } public String getName() { return name; } // 接收警报 public abstract void receiveAlert(Alert alert); } // 医生类 class Doctor extends Staff { public Doctor(String name) { super(name); } // 处理警报 public void receiveAlert(Alert alert) { System.out.println("Doctor " + getName() + " received an alert for patient " + alert.getPatient().getName()); } } // 护士类 class Nurse extends Staff { public Nurse(String name) { super(name); } // 处理警报 public void receiveAlert(Alert alert) { System.out.println("Nurse " + getName() + " received an alert for patient " + alert.getPatient().getName()); } }这串代码运行结果是怎么样的
时间: 2024-01-30 12:04:12 浏览: 139
java.util.ConcurrentModificationException 解决方法
5星 · 资源好评率100%
这段代码是一个简单的医院病房监控系统的实现,它模拟了医院病房中的患者和医护人员,并随机模拟患者出现异常的情况。当患者出现异常时,系统将触发一个警报,并通知所有的医护人员。
具体运行结果取决于代码的调用方式,但是在理想情况下,当患者出现异常时,控制台上应该会输出类似于下面的信息:
```
Doctor John received an alert for patient Alice
Nurse Jane received an alert for patient Alice
```
其中,`John`和`Jane`是医生和护士的名字,`Alice`是出现异常的患者的名字。这些信息表明警报已经成功地通知了医护人员,并且他们已经开始处理这个异常情况。
阅读全文