简体   繁体   English

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

[英]How do you use a method from another class within a class in Java?

So, my assignment is to write a Visit class using Doctor and Patient classes (with their own objects) that has a time and a date and a reference to the Doctor and Patient objects its passed so it can return the same "traits" or variables of the Doctor and Patient classes (ie a Doctor object has a name so the Visit object has to keep that Doctor's name and other variables) At first I wrote that Visit class with its own getSpecialty and getName methods making those variables in Doctor and Patient public. 因此,我的任务是使用Doctor和Patient类(带有自己的对象)编写一个Visit类,该类具有一个时间和一个日期,以及对传递的Doctor和Patient对象的引用,以便它可以返回相同的“特征”或变量的Doctor和Patient类(即Doctor对象具有名称,因此Visit对象必须保留该Doctor的名称和其他变量)首先,我用自己的getSpecialty和getName方法编写了Visit类,从而在Doctor和Patient中公开了这些变量。 The lab instructor wants to have those variables kept private and use the actual methods from Doctor and Patient instead of new methods. 实验室讲师希望使这些变量保持私有状态,并使用Doctor and Patient的实际方法代替新方法。

One thing I researched (this is my first time passing class Objects as a reference and using that class's methods) said as long as you had a reference you could say like cbeta.whatevermethod() but I couldn't make it work. 我研究过的一件事(这是我第一次通过类Objects作为引用并使用该类的方法)说,只要您有引用,您就可以说像cbeta.whatevermethod()一样,但是我无法使其工作。 Java: How to access methods from another class Java:如何从另一个类访问方法

I also don't know if he meant for the method to be in the Visit class or the main class that passes Visit the Doctor and Patient object (but if you call the Doctor's and Patient's objects' methods in the main class anyway, there's no point to pass the objects to the Visit class. 我也不知道他是要让该方法出现在Visit类还是传递Visit Doctor和Patient对象的主类中(但是无论如何,如果您在主类中调用Doctor's和Patient对象的方法,那指向将对象传递给Visit类。

I know this is classwork, so if you don't want to answer it for me that's fine, I'll take any help or guidance because I don't fully understand variable scope and class objects and we're not given time to catch up. 我知道这是课堂作业,所以如果您不想为我回答,那很好,我会寻求帮助或指导,因为我不完全了解变量作用域和类对象,而且我们没有时间来学习起来。 Thanks so much ^_^ 非常感谢^ _ ^

Here's the tester (main class) code: 这是测试器(主类)的代码:

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());
}
}

The Doctor class (I can put patient here too if need be) 医生课(如果需要,我也可以在这里安排患者)

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;
}
 }

And finally, the visit class: 最后,访问类:

public class Visit { private String timeDate; 公共类Visit {private String timeDate; private Doctor d; 私人医生d; private Patient p; 私人患者p;

public Visit()
{
  timeDate = "Time and Date of visit unknown";

}

public Visit (Doctor doc, Patient pat, String thetimeDate)
{
  timeDate = thetimeDate;
  d = doc;
  p = pat;
}
}

It sounds like you should be writing some wrapper methods that expose the methods of the Visit class's composite fields. 听起来您应该编写一些包装方法,以公开Visit类的复合字段的方法。

For example, say you have a class, Dog, that has a name field. 例如,假设您有一个具有名称字段的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;
   }  
}

And suppose you have a class Boy, who "owns" a dog -- who has a Dog field. 并假设您有一个班级Boy,他“拥有”一只狗-他拥有Dog场。 You can give Boy a getDogName() method and have it return the value returned from Dog's getName() method. 您可以给Boy一个getDogName()方法,并让它返回从Dog的getName()方法返回的值。 For example: 例如:

class Boy {
   private Dog dog;

   public Boy(Dog dog) {
      this.dog = dog;
   }

   public String getDogName() {
      return dog.getName();
   }
}

It sounds like you need to do something similar for your Visit class, give Visit methods that call and return the results from methods of its composite fields. 听起来您需要为Visit类执行类似的操作,为Visit方法提供调用并从其复合字段的方法返回结果。

Note, I'm using an example that is different from yours since your question is about a homework assignment, but I have faith that you can generalize the concept. 请注意,由于您的问题与家庭作业有关,因此我使用的示例与您的示例不同,但是我相信您可以概括该概念。

Your question is a little confusing. 您的问题有点令人困惑。 But I believe you are asking how to access the doctor/patient functions through the visit class? 但是我相信您在问如何通过探访课来访问医生/患者的功能?

If so you need to add some public functions to the visit class first. 如果是这样,则需要先向访问类添加一些公共功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM