簡體   English   中英

從其他類調用方法時,Output 未按預期出現

[英]Output not coming out as expected when calling methods from other classes

我正在嘗試創建如下所示的 output:

Here's the first production worker.
Name: John Smith
Employee Number: 123-A
Hire Date: 11-15-2005
Shift: Day
Hourly Pay Rate: $23.50

但是我的代碼導致:

Here's the first production worker.
Name:
Employee Number: INVALID EMPLOYEE NUMBER
Hire Date:
Shift: Day
Hourly Pay Rate: $23.5

這是我使用過的方法和代碼

class Main {
  public static void main(String[] args)
  {
    Employee em = new Employee();
    ProductionWorker pw= new ProductionWorker();

    // First Worker (Testing setting all methods and to string methods)

    System.out.print("Here's the first production worker.");
    pw.setAll("John Smith", "123-A","11-15-2005",1,23.50);
    System.out.print(em.toString());
    System.out.println(pw.toString());
public class Employee{
  private String emName="";
  private String emID="";
  private String emDate="";
  private boolean isIDValid(String ID)
  {
    boolean status = true;
    if (ID.length() != 5)
    {
      status = false;
      emID="";
    }
    else
    {
      if ((!Character.isDigit(ID.charAt(0)))  ||
        (!Character.isDigit(ID.charAt(1)))  ||
        (!Character.isDigit(ID.charAt(2)))  ||
        (ID.charAt(3) != '-')               ||
        (!Character.isLetter(ID.charAt(4))))
        {
            status = false;
            emID="";
        }
    }
    return status;
  } 
  public void setName(String name)
  {
    emName=name;
  }
  public void setID(String ID)
  {
    emID=ID;
  }
  public void setDate(String date)
  {
    emDate=date;
  }
  public String toString()
  {
    System.out.println(emName);
    if (isIDValid(emID))
    {
      return "Name: " + emName + "\nEmployee Number: " + emID + "\nHire Date: " + emDate;
    }
    else
    {
      return "Name: " + emName + "\nEmployee Number: " + "INVALID EMPLOYEE NUMBER" + "\nHire Date: " + emDate;
    }
  }
public class ProductionWorker extends Employee{
  private int emShift;
  private double emPay;
  Employee employee = new Employee();
  public void setAll(String name,String ID,String date,int shift, double pay)
  {
    employee.setName(name);
    employee.setID(ID);
    employee.setDate(date);
    emShift=shift;
    emPay=pay;
  }
  public String toString()
  {
    if ((emShift != 1) && (emShift != 2))
    {
      return "Shift: INVALID SHIFT NUMBER" + "\nHourly Pay Rate: $" + emPay;
    }
    if (emShift==1)
    {
      return  "\nShift: Day" + "\nHourly Pay Rate: $" + emPay;
    }
    else
    {
      return "\nShift: Night" + "\nHourly Pay Rate: $" + emPay;
    }
  }

似乎 emName emDate 和 emID 變量沒有轉換到 em.toString 方法中。 但是,我已經通過在 setter 方法中測試並將它們打印到控制台來測試是否正在設置變量。 在員工 setter 方法和員工 toString 方法之間的某個地方,程序會丟失變量的值。 請幫忙。

你的代碼是錯誤的。 您正在假設變量

Employee employee = new Employee();

在您的ProductionWorker class 內部以某種方式連接到您在Main class 中聲明的那個。

Employee em = new Employee();
ProductionWorker pw= new ProductionWorker();

pw.setAll("John Smith", "123-A","11-15-2005",1,23.50);
System.out.print(em.toString());
System.out.println(pw.toString());

在這段代碼中,您實際上得到了Employee的 2 個實例。 一個稱為em並“存儲”在 Main 內部,另一個位於ProductionWorker實例pw內部,稱為employee 這些是 2 個獨立且斷開連接的實例。

如果你能解決這個問題,那么你會沒事的。 例如,添加一個 getter() 方法來獲取ProductionWorker內部的Employee實例。

更新代碼

Employee class 沒有改變。

生產工人

添加一個 getter ( getEmployee() ) 方法。

package eu.webfarmr.employee;

public class ProductionWorker extends Employee {
    private int emShift;
    private double emPay;
    Employee employee = new Employee();

    public void setAll(String name, String ID, String date, int shift, double pay) {
        this.employee.setName(name);
        this.employee.setID(ID);
        this.employee.setDate(date);
        this.emShift = shift;
        this.emPay = pay;
    }

    public String toString() {
        if ((this.emShift != 1) && (this.emShift != 2)) {
            return "Shift: INVALID SHIFT NUMBER" + "\nHourly Pay Rate: $" + this.emPay;
        }
        if (this.emShift == 1) {
            return "\nShift: Day" + "\nHourly Pay Rate: $" + this.emPay;
        } else {
            return "\nShift: Night" + "\nHourly Pay Rate: $" + this.emPay;
        }
    }

    public Employee getEmployee() {
        return this.employee;
    }
}

主class

移除Employee的本地實例化並調用ProductionWorker上的 getter。

package eu.webfarmr.employee;

public class Main {
    public static void main(String[] args) {
        ProductionWorker pw = new ProductionWorker();

        // First Worker (Testing setting all methods and to string methods)

        System.out.print("Here's the first production worker.");
        pw.setAll("John Smith", "123-A", "11-15-2005", 1, 23.50);
        System.out.print(pw.getEmployee().toString());
        System.out.println(pw.toString());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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