Java建立实体类医生字段有职业、兴趣等,实体类病人字段有年龄、疾病种类等
时间: 2023-07-10 09:05:52 浏览: 110
可以使用以下代码建立Java实体类Doctor和Patient:
```
// Doctor.java
public class Doctor {
private String profession;
private String interest;
public Doctor(String profession, String interest) {
this.profession = profession;
this.interest = interest;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
}
// Patient.java
public class Patient {
private int age;
private String disease;
public Patient(int age, String disease) {
this.age = age;
this.disease = disease;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDisease() {
return disease;
}
public void setDisease(String disease) {
this.disease = disease;
}
}
```
以上代码中,Doctor类有两个私有变量profession和interest,对应医生的职业和兴趣。Patient类有两个私有变量age和disease,对应病人的年龄和疾病种类。每个类都有一个构造函数和对应的getter和setter方法。在构造函数中,可以传入初始变量值并进行初始化。getter和setter方法用于获取和设置私有变量值。
阅读全文