如何在java中的对象之间传递消息

1l5u6lss  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(367)

我试图用oop概念在java中的对象之间传递消息。我创建了两个名为“医生”和“接待员”的类,我希望接待员类的示例向医生的对象发送消息。我还希望类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 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;
    }
}

我怎样才能有一个方法,在课堂上预约病人;当调用该方法时,它会创建一个约会示例?

ibps3vxo

ibps3vxo1#

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

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

相关问题