2014-01-07 54 views
1

我试图创建一个超类,它存储了存储常用方法的类InPatient和OutPatient的详细信息。我设法移动了id域,没有任何问题,但在转向药物治疗时却挣扎不已。在setMedication方法改变将accessor和mutator方法从一个类移动到它的超类

this.medication = medication; 

this.getMedication() = medication; 

有人告诉我,我应该插入一个变量而不是一个值,但无法弄清楚什么变量我应该把在它的位置来代替。

我有类住院:

public class InPatient extends Patient 
{ 
    // The condition of the patient. 
    private String status; 
    // Whether the patient is cured or not. 
    private boolean cured; 

    public InPatient(String base, String id) 
    { 
     status = base; 
     cured = true; 
    } 

    /** 
    * Set the patient's medication 
    * The patient is no longer cured 
    */ 
    public void book(String medication) 
    { 
     setMedication(medication); 
     cured = false; 
    } 

    /** 
    * Show the patients details. 
    */ 
    public String getDetails() 
    { 
     return getID() + " at " + status + " headed for " + 
     getMedication(); 
    } 

    /** 
    * Patient's status. 
    */ 
    public String getStatus() 
    { 
     return status; 
    } 

    /** 
    * Set the patient's medication. 
    */ 
    public void setMedication(String medication) 
    { 
    this.getMedication() = medication; 
    } 

    /** 
    * Show that the patient is cured 
    */ 
    public void better() 
    { 
     status = medication; 
     medication = null; 
     cured = true; 
    } 
} 

,它的超病人:

/** 
* Write a description of class Patient here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class Patient 
{ 
    // Patient's ID. 
    private String id; 
    // Medication the patient is on. 
    private String medication; 

    /** 
    * Constructor for objects of class Patient 
    */ 
    public Patient() 
    { 
     this.id = id; 
     medication = null; 
    } 

    /** 
    * The patient's ID. 
    */ 
    public String getID() 
    { 
     return id; 
    } 

    /** 
    * Patient's medication 
    */ 
    public String getMedication() 
    { 
     return medication; 
    } 
} 

它是什么,我在这里失踪?

回答

0

你不能把一个函数调用在赋值的左侧。这只是没有意义。

如果medication不是private,你可以让你的方法分配给它。由于它是private,你不能。解决办法有两个:

  1. 变化privateprotectedPatient类。这可以让子类直接访问它,而外部客户仍然无法看到medication字段。

  2. (我的首选方法)向父母添加setMedication方法(Patient)。您可以制作protected,这样子女班可以拨打setMedication,而外部客户则不能。无论如何,InPatient类将使用该方法来设置medication(使用语法super.setMedication(medication),以便它调用父类中的方法)。

0

基/父类:

class Patient { 

    private String medication; 

    public String getMedication() { return this.medication; } 
    public String setMedication(String __medication) { 
     this.medication = __medication; 
    } 
} 

子/扩展类:

class InPatient extends Patient { 

    public String getMedication() { super.getMedication(); } 
    public String setMedication(String __medication) { 
     super.setMedication(String __medication); 
    } 
    // if medication is being instantiated using the value from parent class 
    public String setMedication() { 
     this.medication = super.getMedication(); 
    } 
} 
相关问题