Java:由最后一个元素覆盖的对象数组

时间:2015-03-06 00:23:26

标签: java arrays object instances

我遇到了一个有趣的问题,我正在创建一个对象数组,其值在构造函数参数中初始化,但输出(在for循环中)显示初始化的最终元素将覆盖所有其他元素。如果有人可以提供帮助,我会打一个心灵砖墙,我将不胜感激!

public class SoftwareServicesLtd 
{

public static void main(String[] args) 
{  
    Employee.employees[0] = new Employee("John", "Smalls", "Manager", "None", "None", 24000, "Human Resources");
    Employee.employees[1] = new Employee("Devin", "Nathan", "Manager", "None", "None", 24000, "Software Development");
    Employee.employees[2] = new Employee("Rhianna", "Mardsen", "HR Staff", "Devin", "None", 18000, "Human Resources");
    Employee.employees[3] = new Employee("Ryan", "Tepper", "Developer", "John", "C#", 20000, "Software Development");
    Employee.employees[4] = new Employee("Harry", "Wainwright", "Developer", "None", "Java", 15000, "Software Development");

    for(int i= 0; i < Employee.employees.length; i++)
    {
        System.out.println(i + "   -   " + Employee.employees[i].nameFirst_);
    }

员工类和构造函数:

public class Employee extends SoftwareServicesLtd {
    static String nameFirst_, nameLast_, employeeType_, skillSet_, department_ = " ";
    static String[] managers_ = new String[5]; 
    static double salary_ = 0;
    static Employee[] employees = new Employee[5];

public Employee(String first, String last, String employeeType, String manager, String skillSet, double salary, String department)
{
    nameFirst_ = first;
    nameLast_ = last;
    employeeType_ = employeeType;
    managers_[0] = manager;
    skillSet_ = skillSet;
    salary_ = salary;
    department_ = department;
}

预期产出:

0   -   John
1   -   Devin
2   -   Rhianna
3   -   Ryan
4   -   Harry

实际输出:

0   -   Harry
1   -   Harry
2   -   Harry
3   -   Harry
4   -   Harry

0 个答案:

没有答案