簡體   English   中英

您如何使用Java類中另一個類的方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM