簡體   English   中英

如何在 Java 中的對象之間傳遞消息

[英]How can I pass messages between objects in Java

我試圖在具有 OOP 概念的 Java 對象之間傳遞消息。 我創建了兩個名為 Doctor 和 Receptionist 的類,我希望 Receptionist 類的實例向 Doctor 的對象發送消息。 我還希望 Patient 類的對象向 Appointments 類的對象發送消息(預約約會)。

綜上所述,我想實現不同類的不同實例之間的關系和通信。

病人班

public class Patient 
{
    private int id;
    private String name;
    private int age;
    private String condition;

    public Patient (int id, String name, int age, String condition)
    {
        this.setId(id);
        this.setName("name");
        this.setAge(age);
        this.setCondition("condition");
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public int getId()
    {
        return this.id;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return this.age;
    }
    public void setCondition(String condition)
    {
        this.condition = condition;
    }
    public String getCondition()
    {
        return this.condition;
    }
}

預約班

public class Appointment {
    private int appointId;
    private String date;
    private String purpose;
    
    public Appointment (int appointId, String date, String purpose)
    {
        this.setAppointId(appointId);
        this.setDate("date");
        this.setPurpose("purpose");
    }
    public void setAppointId(int appointId)
    {
        this.appointId = appointId;
    }
    public void setDate(String date)
    {
        this.date = date;
    }
    public void setPurpose(String purpose)
    {
        this.purpose = purpose;
    }
}

我怎樣才能有一種方法可以在 Patient 類中預約; 當該方法被調用時,它會創建一個 Appointment 的實例?

您的 Patient 類可能如下所示:

public class Patient {
    private int id;
    private String name;
    private int age;
    private String condition;

    public Patient( int id, String name, int age, String condition ) {
        this.setId(id);
        this.setName("name");
        this.setAge(age);
        this.setCondition("condition");
    }

    public void setId( int id ) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public void setName( String name ) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setAge( int age ) {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }

    public void setCondition( String condition ) {
        this.condition = condition;
    }

    public String getCondition() {
        return this.condition;
    }

    public Appointment bookAnAppointment( int appointId, String date, String purpose ) {
        return new Appointment(appointId, date, purpose);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM