2014-03-13 21 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; 
} 

}