![](/img/trans.png)
[英]Java - How do I get a variable generated from within a method in one class to another class?
[英]How do you use a method from another class within a class in Java?
因此,我的任务是使用Doctor和Patient类(带有自己的对象)编写一个Visit类,该类具有一个时间和一个日期,以及对传递的Doctor和Patient对象的引用,以便它可以返回相同的“特征”或变量的Doctor和Patient类(即Doctor对象具有名称,因此Visit对象必须保留该Doctor的名称和其他变量)首先,我用自己的getSpecialty和getName方法编写了Visit类,从而在Doctor和Patient中公开了这些变量。 实验室讲师希望使这些变量保持私有状态,并使用Doctor and Patient的实际方法代替新方法。
我研究过的一件事(这是我第一次通过类Objects作为引用并使用该类的方法)说,只要您有引用,您就可以说像cbeta.whatevermethod()一样,但是我无法使其工作。 Java:如何从另一个类访问方法
我也不知道他是要让该方法出现在Visit类还是传递Visit Doctor和Patient对象的主类中(但是无论如何,如果您在主类中调用Doctor's和Patient对象的方法,那指向将对象传递给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;
}
}
最后,访问类:
公共类Visit {private String timeDate; 私人医生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类的复合字段的方法。
例如,假设您有一个具有名称字段的Dog类。
class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
并假设您有一个班级Boy,他“拥有”一只狗-他拥有Dog场。 您可以给Boy一个getDogName()
方法,并让它返回从Dog的getName()方法返回的值。 例如:
class Boy {
private Dog dog;
public Boy(Dog dog) {
this.dog = dog;
}
public String getDogName() {
return dog.getName();
}
}
听起来您需要为Visit类执行类似的操作,为Visit方法提供调用并从其复合字段的方法返回结果。
请注意,由于您的问题与家庭作业有关,因此我使用的示例与您的示例不同,但是我相信您可以概括该概念。
您的问题有点令人困惑。 但是我相信您在问如何通过探访课来访问医生/患者的功能?
如果是这样,则需要先向访问类添加一些公共功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.