【问题标题】:Printing an Array of Staff objects with a toString使用 toString 打印人员对象数组
【发布时间】:2014-03-13 19:23:38
【问题描述】:

在我的驱动程序中使用定义的 toString 方法打印出 StaffMember 对象数组时遇到问题。我不断收到找不到符号错误,我很困惑我需要在我的驱动程序中用什么替换 staffList 以使事情顺利进行。

这是我遇到的问题的一部分:“您的程序应首先将所有员工(使用 StaffMember 类的 toString() 方法)打印到终端窗口”

这是我的代码(Staff 和 StaffMember 课程来自教科书,不需要为作业进行更改,所以所有问题都与我的驱动程序有关)。

public class Staff
{
  private StaffMember[] staffList;

  public Staff ()
  {
    staffList = new StaffMember[6];

    staffList[0] = new Executive ("Sam", "123 Main Line",
    "555-0469", "123-45-6789", 2423.07);

    staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101",
    "987-65-4321", 1246.15);

    staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000",
    "010-20-3040", 1169.23);

    staffList[3] = new Hourly ("Diane", "678 Fifth Ave.",
    "555-0690", "958-47-3625", 10.55);

    staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.",
    "555-8374");

    staffList[5] = new Volunteer ("Cliff", "321 Duds Lane",
    "555-7282");

    ((Executive)staffList[0]).awardBonus (500.00);

    ((Hourly)staffList[3]).addHours (40);
  }

    public void payday ()
    {
      double amount;

      for (int count=0; count < staffList.length; count++)
      {
        System.out.println (staffList[count]);
        amount = staffList[count].pay();

        if (amount == 0.0)
          System.out.println ("Thanks!");
        else
          System.out.println ("Paid: " + amount);
        System.out.println ("-----------------------------------");
      }
    }
 }

这是抽象类:

abstract public class StaffMember
{
  protected String name;
  protected String address;
  protected String phone;
//-----------------------------------------------------------------
// Constructor: Sets up this staff member using the specified
// information.
//-----------------------------------------------------------------
  public StaffMember (String eName, String eAddress, String ePhone)
  {
    name = eName;
    address = eAddress;
    phone = ePhone;
  }
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
  public String toString()
  {
    String result = "Name: " + name + "\n";
    result += "Address: " + address + "\n";
    result += "Phone: " + phone;
    return result;
  }
//-----------------------------------------------------------------
// Derived classes must define the pay method for each type of
// employee.
//-----------------------------------------------------------------
  public abstract double pay();
}

到目前为止,这是我为司机得到的:

import java.util.*;
public class EmployeeBinaryList
{
  public static void main (String args[])
  {
    for (int i = 0; i < staffList.length; i++)
    System.out.println(staffList[i].toString());
  }
}

我已经尝试了各种方法来代替 staffList 和 staffList[i],但我似乎无法弄清楚。非常感谢任何可以帮助我的人

【问题讨论】:

  • 您的EmployeeBinaryList#main 中的staffList 是什么?
  • 另外,System.out.println(staffList[i]);System.out.println(staffList[i].toString()); 更安全,因为您可能会暴露您的程序以引发 NPE。
  • staffList 是 Staff 类中包含我需要打印的所有员工信息的数组的名称。
  • 在你的主方法类中创建一个 Staff 类的对象,即 EmployeeBinaryList,然后从 Staff 对象中获取 Stafflist 数组。

标签: java arrays object tostring


【解决方案1】:

您需要在这里考虑范围。变量范围是可以访问该变量的位置。了解变量范围的最简单方法是使用花括号。变量只能在定义它的大括号内直接访问。因此,staffList 是在 staff 类中定义的,因此它只能在 staff 类中直接访问。

您必须通过人员类的对象访问该变量:

System.out.println(StaffObject.StaffList) //StaffObject would be an object of the Staff class

但是,在这种情况下,您还需要查看变量是公共的还是私有的。私有意味着它不能直接在其类之外访问。所以在这种情况下,StaffObject.staffList 将无法在 Staff 类之外访问

为了访问 StaffList 变量,您需要所谓的 Accessor 方法。一种公开的方法,允许访问变量以进行打印。

所以,这是必须做的:

首先你需要一个人员类的对象 然后,您需要使用该对象访问适当的访问器方法进行打印

好好看看代码,很可能是有原因的。

祝你好运!!

【讨论】:

    【解决方案2】:
    package com.cisco.staff;
    

    公开课工作人员 { 私人 StaffMember[] 员工列表;

    公职人员 () { 员工列表 = 新员工成员[6];

    staffList[0] = new Executive ("Sam", "123 Main Line",
    "555-0469", "123-45-6789", 2423.07);
    
    staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101",
    "987-65-4321", 1246.15);
    
    staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000",
    "010-20-3040", 1169.23);
    
    staffList[3] = new Hourly ("Diane", "678 Fifth Ave.",
    "555-0690", "958-47-3625", 10.55);
    
    staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.",
    "555-8374");
    
    staffList[5] = new Volunteer ("Cliff", "321 Duds Lane",
    "555-7282");
    

    /* ((Executive)staffList[0]).awardBonus (500.00);

    ((Hourly)staffList[3]).addHours (40);*/
    

    }

    public void payday ()
    {
      double amount;
    
      for (int count=0; count < staffList.length; count++)
      {
        System.out.println (staffList[count]);
        amount = staffList[count].pay();
    
        if (amount == 0.0)
          System.out.println ("Thanks!");
        else
          System.out.println ("Paid: " + amount);
        System.out.println ("-----------------------------------");
      }
    }
    
    public StaffMember[] getStaffList() {
        return staffList;
    }
    
    public void setStaffList(StaffMember[] staffList) {
        this.staffList = staffList;
    }
    

    }

    package com.cisco.staff;
    

    抽象公共类StaffMember { 受保护的字符串名称; 受保护的字符串地址; 受保护的字符串电话;

    //--------------------------------------------- -------------------- // 构造函数:使用指定的设置这个职员 // 信息。 //------------------------------------------------ ----------------- public StaffMember(字符串 eName、字符串 eAddress、字符串 ePhone) { 姓名 = 易名; 地址 = 电子地址; 电话=手机; }

    //--------------------------------------------- -------------------- // 返回一个包含基本员工信息的字符串。 //------------------------------------------------ ----------------- 公共字符串 toString() { 字符串结果 = “名称:” + 名称 + “\n”; 结果+=“地址:”+地址+“\n”; 结果+=“电话:”+电话; 返回结果; } //------------------------------------------------ ----------------- // 派生类必须为每种类型定义支付方法 // 员工。 //------------------------------------------------ ----------------- 公共抽象双薪(); }

    package com.cisco.staff;
    

    导入 java.util.List;

    公共类 EmployeeBinaryList { 公共静态无效主要(字符串args []) { 员工员工 = 新员工(); StaffMember[] staffList = staff.getStaffList(); for (int i = 0; i

    package com.cisco.staff;
    

    公共类Executive扩展StaffMember { 受保护的字符串 somestrString; 受保护的双倍;

    public Executive(String eName, String eAddress, String ePhone, String someString , double pay) {
        super(eName, eAddress, ePhone);
        this.somestrString= someString;
        this.somelong=pay;
        // TODO Auto-generated constructor stub
    }
    
    
    
    @Override
    public double pay() {
        // TODO Auto-generated method stub
        return 0;
    }
    

    }

    package com.cisco.staff;
    

    公共类志愿者扩展 StaffMember { 受保护的字符串 somestrString; 受保护的双倍;

    public Volunteer(String name, String address, String phone) {
        super(name, address, phone);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public double pay() {
        // TODO Auto-generated method stub
        return 0;
    }
    

    }

    package com.cisco.staff;
    
    public class Employee extends StaffMember {
      protected String somestrString;
      protected double somelong;
    
    public Employee(String name, String address, String phone,
            String somestrString, double somelong) {
        super(name, address, phone); 
        this.somestrString=somestrString;
        this.somelong=somelong;
    
    }
    
    @Override
    public double pay() {
        // TODO Auto-generated method stub
        return 0;
    }
    

    }

    package com.cisco.staff;
    
     public class Hourly extends StaffMember {
      protected String somestrString;
      protected double hourly;
    
    public Hourly(String eName, String eAddress, String ePhone, String somString, double hourly) {
        super(eName, eAddress, ePhone);
        this.somestrString=somString;
        this.hourly=hourly;
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public double pay() {
        // TODO Auto-generated method stub
        return 0;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多