帮我用java写医院管理系统中名为Person的抽象类,其中里面要有三个子类来表示患者、医生和护士
时间: 2024-02-12 11:09:00 浏览: 68
使java写的医院管理系统
5星 · 资源好评率100%
好的,我可以提供一个简单的实现,如下所示:
```java
public abstract class Person {
private String name;
private int age;
private String id;
public Person(String name, int age, String id) {
this.name = name;
this.age = age;
this.id = id;
}
// Getters and setters for name, age, and id
public abstract String getOccupation();
}
public class Patient extends Person {
private String medicalHistory;
public Patient(String name, int age, String id, String medicalHistory) {
super(name, age, id);
this.medicalHistory = medicalHistory;
}
// Getter and setter for medicalHistory
public String getOccupation() {
return "Patient";
}
}
public class Doctor extends Person {
private String specialty;
public Doctor(String name, int age, String id, String specialty) {
super(name, age, id);
this.specialty = specialty;
}
// Getter and setter for specialty
public String getOccupation() {
return "Doctor";
}
}
public class Nurse extends Person {
private int yearsOfExperience;
public Nurse(String name, int age, String id, int yearsOfExperience) {
super(name, age, id);
this.yearsOfExperience = yearsOfExperience;
}
// Getter and setter for yearsOfExperience
public String getOccupation() {
return "Nurse";
}
}
```
在这个实现中,我们定义了一个抽象类 `Person`,它包含了一些基本的人员信息,例如姓名、年龄和身份证号。我们还定义了一个抽象方法 `getOccupation`,用于返回人员的职业类型。
然后,我们创建了三个子类 `Patient`、`Doctor` 和 `Nurse`,它们分别表示患者、医生和护士。每个子类都继承了 `Person` 类,并且实现了 `getOccupation` 方法以返回相应的职业类型。
当你需要在医院管理系统中创建患者、医生或护士对象时,可以使用这些类来表示它们。例如:
```java
Person patient = new Patient("张三", 30, "123456", "高血压");
Person doctor = new Doctor("李四", 40, "234567", "心脏病专家");
Person nurse = new Nurse("王五", 25, "345678", 2);
```
阅读全文