有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中,如何使用类中另一个类的方法?

所以,我的任务是使用Doctor和Patient类(带有它们自己的对象)编写一个访问类,该类有一个时间和日期以及对其传递的Doctor和Patient对象的引用,因此它可以返回Doctor和Patient类的相同“特征”或变量(即医生对象有一个名称,因此访问对象必须保留该医生的名称和其他变量)首先,我使用自己的getSpeciality和getName方法编写了访问类,使这些变量在医生和患者中公开。实验室讲师希望这些变量保持私有,并使用医生和患者提供的实际方法,而不是新方法

我研究的一件事(这是我第一次将类对象作为引用传递并使用该类的方法)是,只要有一个引用,就可以像cbeta那样说。whatevermethod()但我无法让它工作Java: How to access methods from another class

我也不知道他是想让方法在Visit类中还是在传递Visit医生和病人对象的主类中(但如果您在主类中调用医生和病人对象的方法,那么将对象传递给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;
}
}

共 (2) 个答案

  1. # 1 楼答案

    听起来您应该编写一些包装器方法来公开Visit类的复合字段的方法

    例如,假设您有一个类Dog,它有一个name字段

    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一个getDogName()方法,让它返回Dog的getName()方法返回的值。例如:

    class Boy {
       private Dog dog;
    
       public Boy(Dog dog) {
          this.dog = dog;
       }
    
       public String getDogName() {
          return dog.getName();
       }
    }
    

    听起来你需要为你的Visit类做一些类似的事情,给Visit方法调用并返回其复合字段的方法的结果

    注意,我使用的例子与你的不同,因为你的问题是关于家庭作业的,但我相信你可以概括这个概念

  2. # 2 楼答案

    你的问题有点让人困惑。但我相信你是在问如何通过访问类访问医生/患者功能?

    如果是这样的话,你需要先在visit类中添加一些公共函数