我无法通过使用我的驱动程序中定义的toString方法打印出一个StaffMember对象数组。我一直在找一个找不到符号的错误,我很困惑,我需要用我的驱动程序替换staffList以使事情顺利进行。使用toString打印一个Staff对象数组
这是我卡上的问题的一部分“你的程序应该首先所有的工作人员(使用toString()将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 [我],但我似乎无法弄清楚。非常感谢任何能够帮助我的人
是什么'staffList'在'EmployeeBinaryList#main'? – 2014-03-13 19:25:30
另外''System.out.println(staffList [i]);'比'System.out.println(staffList [i] .toString())'更安全,因为你可能会暴露你的程序来抛出NPE。 –
staffList是Staff类中数组的名称,它包含我需要打印的所有员工信息。 – user3417012