【发布时间】:2014-09-11 01:50:57
【问题描述】:
因此,我的任务是使用 Doctor 和 Patient 类(带有自己的对象)编写一个 Visit 类,该类具有时间和日期以及对传递的 Doctor 和 Patient 对象的引用,因此它可以返回相同的“特征" 或 Doctor 和 Patient 类的变量(即 Doctor 对象有一个名称,因此 Visit 对象必须保留该 Doctor 的名称和其他变量)起初我用它自己的 getSpecialty 和 getName 方法编写了该 Visit 类,使这些变量在 Doctor和患者公众。实验室讲师希望将这些变量保密,并使用 Doctor 和 Patient 的实际方法而不是新方法。
我研究过的一件事(这是我第一次将类 Objects 作为引用传递并使用该类的方法)说,只要你有一个引用,你可以说像 cbeta.whatevermethod() 但我不能让它工作. Java: How to access methods from another class
我也不知道他的意思是该方法是在 Visit 类中还是在传递 Visit the Doctor 和 Patient 对象的主类中(但如果你在主类中调用 Doctor's 和 Patient's 对象的方法,没有必要将对象传递给 Visit 类。
我知道这是课堂作业,所以如果你不想为我回答也没关系,我会接受任何帮助或指导,因为我不完全理解变量范围和类对象,而且我们没有给出是时候赶上了。非常感谢^_^
这是测试器(主类)代码:
public class tester
{
public static void main(String[] args)
{
Doctor doctorStrange
= new Doctor("General Practitioner", 50.0);
doctorStrange.setName("Doctor Strange");
Doctor doctorFate
= new Doctor("Pediatrician");
doctorFate.setName("Doctor Fate");
Doctor doctorLight
= new Doctor();
doctorLight.setName("Doctor Light");
Patient Thor
= new Patient(42154);
Thor.setName("Thor");
Patient Bruce
= new Patient(67245);
Bruce.setName("Bruce");
Patient Clint
= new Patient();
Clint.setName("Clint");
Visit visit1 = new Visit(doctorStrange, Thor, "9:53 AM, 2/27/2014");
Visit visit2 = new Visit(doctorStrange, Bruce, "4:22 PM, 7/13/2017");
Visit visit3 = new Visit(doctorFate, Clint, "8:59 AM, 5/05/2015");
System.out.println("First visit: Doctor name is " + doctorStrange.getName()
+ " and Patient name is " + Thor.getName());
}
}
医生类(如果需要我也可以把病人放在这里)
public class Doctor extends Person
{
public double visitFee;
public String specialty;
public String name;
public Doctor ()
{
visitFee = 0.0;
specialty = "unknown";
}
public Doctor (String type)
{
specialty = type;
visitFee = 0.0;
}
public Doctor (double initialFee)
{
visitFee = initialFee;
specialty = "unknown";
}
public Doctor (String type, double initialFee)
{
specialty = type;
visitFee = initialFee;
}
public void setName(String newName)
{
name = newName;
}
public String getSpecialty ()
{
return specialty;
}
public double getVisitFee ()
{
return visitFee;
}
public String getName()
{
return name;
}
}
最后是访问类:
公开课参观 { 私人字符串时间日期; 私人医生 d; 私人患者 p;
public Visit()
{
timeDate = "Time and Date of visit unknown";
}
public Visit (Doctor doc, Patient pat, String thetimeDate)
{
timeDate = thetimeDate;
d = doc;
p = pat;
}
}
【问题讨论】:
-
我认为您最大的问题是您可能不了解您的作业。因此,在该说明中,请为我们充分澄清。您的书面说明具体说明了您在访问中需要具备哪些内容,以及您是如何陷入困境的?你的问题不清楚,至少对我来说不是。您当然确实需要为 Visit 提供一些方法,并且您可能需要在这些 Visit 方法中调用 Doctor 和 Patient 的方法。
-
对不起,我编辑了第一段希望让它更清楚。本质上,访问类需要传递对医生和患者类的引用[我的构造函数是 public Visit (Doctor doc, Patient pat, String thetimeDate)] 而且我需要能够使用原始方法(如 getName 方法) 从我的访问类中的医生或患者类返回到我的主程序信息,如名称。我希望这更清楚。我也感谢你的帮助:)
标签: java class inheritance reference