在 Java 中的不同 类 中写入对象

Writing objects in different classes in Java

Define two classes, Patient and Billing, whose Objects are records for a clinic. Derive patient from the class person. A class Person has a name, it has methods for setting and getting the name. It also has a function to display the output and a method hasSameName that checks if names are equal. A patient record has the patients name(defined in the class person) and identification number(use the type String). A Billing object will contain a patient object and a doctor object A Doctor record has the doctor’s name—defined in the class Person—a specialty as a string (for example Paediatrician, Obstetrician, General Practitioner, and so on), and an office visit fee (use the type double). Give your Patient and Billing classes a reasonable complement of constructors and accessor methods, and an equal’s method as well. Write a test program that creates at least two patients, at least two doctors and create an array of type Person and process the objects polymorphically. Then create at least two billing records and then displays the total income from the billing records.

这显然是我正在努力的硬件,具体来说我应该在 Billing 中写什么 class?我应该如何关联患者记录和账单以及医生记录?

为了回答您关于 Billing class 的具体问题以及您如何关联 PatientDoctor,我认为您的 Billing class 可能看起来像这样(开始):

public class Billing {

private Patient p;
private Doctor dr;

public Billing(Patient p, Doctor dr) {
    this.p = p;
    this.dr = dr;
}

public Doctor getDoctor() {
    return dr;
}

public void setDoctor(Doctor newDoctor) {
    dr = newDoctor;
}

public Patient getPatient() {
    return p;
}

public void setPatient(Patient newPatient) {
    p = newPatient;
}
}

我希望这已经给了你一个正确方向的推动。

编辑:根据第一个建议改进了代码。