2014-03-13 22 views
0

我無法通過使用我的驅動程序中定義的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 [我],但我似乎無法弄清楚。非常感謝任何能夠幫助我的人

+1

是什麼'staffList'在'EmployeeBinaryList#main'? – 2014-03-13 19:25:30

+2

另外''System.out.println(staffList [i]);'比'System.out.println(staffList [i] .toString())'更安全,因爲你可能會暴露你的程序來拋出NPE。 –

+0

staffList是Staff類中數組的名稱,它包含我需要打印的所有員工信息。 – user3417012

回答

0

您需要考慮範圍。變量作用域是可以訪問變量的地方。知道變量範圍的最簡單方法是用花括號。一個變量只能在其定義的大括號內直接訪問。因此,staffListstaff類中定義,因此它只能在staff類中直接訪問。

你就必須通過工作人員類的一個對象來訪問該變量:

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

然而,在這種情況下,你還需要看看變量是否是公共的還是私有的。私人意味着它不能在課堂外直接訪問。所以在這種情況下StaffObject.staffList不會

Staff類的外部訪問。爲了訪問StaffList變量,你需要什麼所謂的Accessor方法。一種公開的方法,允許訪問該變量以進行打印。

所以,這裏是你會怎樣得做:

首先你需要的工作人員類 的對象,然後,你需要使用該對象來訪問相應的訪問方法打印

好好看看在代碼中,所有這些都是有原因的。

祝你好運!

0
package com.cisco.staff; 

公共類職員 { 私人StaffMember [] staffList;

公職人員() { 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 ("-----------------------------------"); 
    } 
} 

public StaffMember[] getStaffList() { 
    return staffList; 
} 

public void setStaffList(StaffMember[] staffList) { 
    this.staffList = staffList; 
} 

}

package com.cisco.staff; 

抽象公共類StaffMember { 保護字符串名稱; 保護字符串地址; 保護絃樂手機;

// -------------------------------------------- --------------------- //構造函數:使用指定的 //信息設置此職員。 // ----------------------------------------------- ------------------ 公共StaffMember(字符串易名,字符串eAddress,字符串EPHONE) { 名稱=易名; address = eAddress; phone = ePhone; }

// ------------------------------------------ ----------------------- //返回一個包含基本員工信息的字符串。 // ----------------------------------------------- ------------------ 公共字符串的toString() { 字符串結果= 「名稱:」 +名稱+ 「\ n」 個; 結果+ =「地址:」+地址+「\ n」; 結果+ =「電話:」+電話; 返回結果; } // --------------------------------------------- -------------------- //派生類必須定義每種類型的 //員工的支付方法。 // ----------------------------------------------- ------------------ public abstract double pay(); }

package com.cisco.staff; 

import java.util.List;

公共類EmployeeBinaryList { 公共靜態無效主要(字符串ARGS []){ 職員 人員=新員工(); StaffMember [] staffList = staff.getStaffList(); (int i = 0;staffList.length; i ++) System.out.println(staffList [i]。的toString()); }}

package com.cisco.staff; 

公共類行政擴展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; 
} 

}