如何使用多态创建一个arraylist?

时间:2017-03-22 02:12:10

标签: java arraylist

我正在尝试创建一个包含所有员工的数组列表,并且能够处理任何类型的员工。我还必须将数据加载到列表中我正在使用的类称为工资单。这就是我到目前为止所做的:

员工类看起来像这样:

 import java.util.*;
 public abstract class Employee
{
private String name, employeeNum, department;
private char type;

public Employee()
{
    name ="";
    employeeNum = "";
    department = "";
 }

public Employee(String Name, String EmpNum, String Depart)
{
   name = Name;
   employeeNum = EmpNum;
   department = Depart;
}

//public EMpoy

public String getName()
{
    return name;
}

public String getEmployeeNum()
{
    return employeeNum;
}

public String getDepartment()
{
    return department;
}

public char getType()
{
    return type;
}

public void setName(String Name)
{
   name = Name; 
}

public void setEmployeeNum(String EmpNum)
{
    employeeNum = EmpNum;
}

public void setDepartment(String Depart)
{
    department = Depart;
}

public String toString()
{
    String str;

    str = "Employee Name: " + name + "\n" 
         + "Employee Number: " + employeeNum + "\n"
         + "Employee Department: " + department + "\n";

    return str;
}

}

到目前为止,工资单类看起来像这样:

import java.util.*;
import java.io.*;

public class Payroll
{
private ArrayList<Employee> list = new ArrayList<Employee>();

private String fileName;

public Payroll()
{

}

public void fileName(String[] args)
{
    Scanner kb = new Scanner(System.in);
    System.out.println("InsertFileName");
    String fileName1 = kb.next();
    fileName = fileName1 + ".txt";
}

public void loadData() throws FileNotFoundException
{
    Scanner s = new Scanner(new File(fileName));

    while (s.hasNext())
      {
          String name = s.next();
          String employeeNum = s.next();
          String department = s.next();
          //String typeString = s.next();
          //char type = typeString.toUpperCase().charAt(0);
          char type = s.next().toUpperCase().charAt(0);

            if (type == 'S')
                {
                    double yearlySalary = s.nextDouble();

                    list.add(new Salary (name, employeeNum, department, yearlySalary));
                }
            else if (type == 'H')    
                {
                       double hourlyPayRate = s.nextDouble();
                       String hours = s.next();
                       int hoursWorked = Integer.parseInt(hours);

                       list.add(new Hourly (name, employeeNum, department, hourlyPayRate, hoursWorked));
                }
             else if (type == 'C')
                {
                    int numOfWeeks = s.nextInt();
                    double baseWeeklySalary = s.nextDouble();
                    int salesThisWeek = s.nextInt();
                    int salesThisYear = s.nextInt();
                    double commissionRate = s.nextDouble();

                    list.add(new Commission (name, employeeNum, department,     numOfWeeks, baseWeeklySalary, salesThisWeek, salesThisYear, commissionRate));
                }
        }

    s.close();  
  }

现在我知道我应该在构造函数中创建arraylist,这就是我遇到的麻烦。如何使用多态来使列表获得每个员工?感谢。

1 个答案:

答案 0 :(得分:0)

您好Srk93您收到错误,因为您的列表包含Employee类的引用,而Employee类没有getCommissionRate方法。您可以调用Employee类中声明的Employee引用。创建calculateSalary()的abstact方法并在所有子类中实现。

它是"cannot find symbol: method" but the method is declared

的副本