2014-01-07 2 views
1

일반적인 메서드를 저장하는 클래스 InPatient 및 OutPatient에 대한 세부 정보를 저장하는 수퍼 클래스를 만들려고합니다. 나는 id 필드를 넘어서 아무 문제없이 움직일 수 있었지만 약물 치료를하는 데 있어서는 어려움을 겪고있다. setMedication 방법에 내가 변수 대신 값을 삽입해야하지만 대신 장소에 나는 퍼팅해야 어떤 변수 알아낼 수 없다는 이야기입니다한 클래스에서 수퍼 클래스로 액세서 및 변경자 메서드 이동

this.getMedication() = medication; 

this.medication = 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

당신은 할당의 왼쪽에있는 함수 호출을 넣을 수 없습니다. 그것은 단지 의미가 없습니다.

이 아닌 경우에는 방법을 할당 할 수 있습니다. 그것이 private이기 때문에, 당신은 할 수 없습니다. Patient 클래스 protected

  1. 변경 private 두 가지 해결책이 있습니다. 외부 클라이언트는 여전히 medication 필드를 볼 수 없지만 하위 클래스는이를 통해 직접 클래스에 액세스 할 수 있습니다.

  2. (선호하는 방법) 부모에게 setMedication 메서드를 추가하십시오 (Patient). 하위 클래스가 setMedication을 호출 할 수 있도록이 클라이언트를 protected으로 만들 수 있습니다. 어쨌든 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(); 
    } 
}