Java 将访问器和变异器方法从一个类移动到其超类
我正在尝试创建一个超类,该超类同时存储类inquired和outmount的详细信息,后者存储常用方法。我已经成功地在id字段上移动,没有任何问题,但是在移动药物时,我很挣扎。关于变更时的用药方法Java 将访问器和变异器方法从一个类移动到其超类,java,inheritance,superclass,Java,Inheritance,Superclass,我正在尝试创建一个超类,该超类同时存储类inquired和outmount的详细信息,后者存储常用方法。我已经成功地在id字段上移动,没有任何问题,但是在移动药物时,我很挣扎。关于变更时的用药方法 this.medication = medication; 到 我被告知我应该插入一个变量而不是一个值,但我无法确定应该将哪个变量放在它的位置 我的班级是住院病人: public class InPatient extends Patient { // The condition of
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;
}
}
我在这里遗漏了什么?基类/父类:
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();
}
}
不能将函数调用放在赋值的左侧。这根本没有道理 如果
药物
不是私有的
,您可以将您的方法分配给它。因为它是私有的,所以你不能。有两种解决方案:
Patient
类中将private
更改为protected
。这样子类就可以直接访问它,而外部客户端仍然看不到药物
字段setmedicing
方法添加到父对象(Patient
)。您可以将此设置为受保护的
,以便子类可以调用setmedicing
,而外部客户端则不能。在任何情况下,Inspitality
类都将使用该方法设置药物
(使用语法super.setmedicing(medicing)
,以便在父类中调用该方法)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();
}
}