我無法通過使用我的驅動程序中定義的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