当前位置:  开发笔记 > 编程语言 > 正文

从其他类调用方法时输出未按预期输出

如何解决《从其他类调用方法时输出未按预期输出》经验,为你挑选了1个好方法。

我正在尝试创建如下所示的输出:

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方法中测试并将它们打印到控制台中来设置变量。在雇员设置方法和雇员toString方法之间的某个位置,程序会丢失变量的值。请帮忙。



1> David Brossa..:

您的代码是错误的。您正在假设该变量

Employee employee = new Employee();

在您的ProductionWorker班级内部以某种方式与您在Main班级中声明的内容相关联。

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

在此代码中,您实际上最终得到了2个实例Employee。一个em在Main内部调用并“存储”,另一个在ProductionWorker实例内部pw调用employee。这是2个独立且断开连接的实例。

如果您解决了该问题,那么您会没事的。例如,添加一个getter()方法来获取Employee您内部的实例ProductionWorker

更新的代码

Employee级不会改变。

生产工人

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

主班

删除的本地实例,Employee然后在上调用getter ProductionWorker

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

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有