我在使用驱动程序定义的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],但我似乎无法弄明白。非常感谢任何可以帮助我的人
答案 0 :(得分:0)
你需要在这里考虑范围。变量范围是可以访问该变量的位置。知道变量范围的最简单方法是使用花括号。变量只能在定义它的大括号内直接访问。因此,staffList
在staff
类中定义,因此只能在staff
类中直接访问。
您必须通过职员类的对象访问该变量:
System.out.println(StaffObject.StaffList) //StaffObject would be an object of the Staff class
但是,在这种情况下,您还需要查看变量是公共变量还是私有变量。私有意味着它不能直接在其类之外访问。因此,在这种情况下,StaffObject.staffList
类
Staff
要访问StaffList
变量,您需要所谓的Accessor
方法。一种公共方法,允许访问变量以进行打印。
所以,这里有什么要做的:
首先,您需要一个员工班级的对象 然后,您需要使用该对象访问适当的Accessor方法进行打印
仔细看看代码,很可能是因为这一切。
祝你好运!答案 1 :(得分:0)
package com.cisco.staff;
公共班级工作人员 { private 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");
/ *((执行)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 { protected String name; protected String address; protected String phone;
// --------------------------------------------- -------------------- //构造函数:使用指定的人员设置此人员 //信息 // ------------------------------------------------ ----------------- public StaffMember(String eName,String eAddress,String ePhone) { name = eName; address = eAddress; phone = ePhone; }
// --------------------------------------------- -------------------- //返回包含基本员工信息的字符串。 // ------------------------------------------------ ----------------- public String toString() { 字符串结果=&#34;名称:&#34; +名称+&#34; \ n&#34 ;; 结果+ =&#34;地址:&#34; +地址+&#34; \ n&#34 ;; 结果+ =&#34;电话:&#34; +电话; 返回结果; } // ------------------------------------------------ ----------------- //派生类必须为每种类型定义付费方式 //员工 // ------------------------------------------------ ----------------- public abstract double pay(); }
package com.cisco.staff;
import java.util.List;
公共类EmployeeBinaryList { public static void main(String args []) { 员工=新员工(); StaffMember [] staffList = staff.getStaffList(); for(int i = 0; i&lt; staffList.length; i ++) 的System.out.println(staffList [I]的ToString()); } }
package com.cisco.staff;
公共班主任延伸职员会员{ protected String 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;
公共课志愿者延伸职员会员{ protected String 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;
}
}