2014-01-07 72 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(); 
    } 
} 
相關問題