【问题标题】:How can I pass messages between objects in Java如何在 Java 中的对象之间传递消息
【发布时间】:2021-09-17 19:42:34
【问题描述】:

我正在尝试使用 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 的实例?

【问题讨论】:

  • 您可以创建一个以Appointment 作为返回类型的方法。在方法中创建一个new Appointment 并返回它。请记住,如果您在 Patient 中创建此方法,则必须在其中“包含”“约会”才能被识别
  • 实际上,这就是依赖注入发挥作用的地方。但是,如果您只是想看看,那么我将在下面提供一个示例。
  • Java 中的“传递消息” = 调用方法。
  • 这能回答你的问题吗? What is message passing in OOP?
  • @CelestineAkpanoko 你可以知道在调用 bookAnAppointment() 方法时创建了约会............该方法将返回一个约会对象......所以,你可以将 bookAnAppointment() 的返回值存储在一个数组中............

标签: java oop class-method object-relationships


【解决方案1】:

您的患者类可能如下所示:

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多