Java:ArrayList中的两个方法的可访问性问题(三个类)

时间:2017-10-06 18:01:13

标签: java methods

我有三个课程:MainReusaxCorpEmployee。让我们直截了当地说:在ReusaxCorp类中,我想实现两种方法:retrieveEmployee,它遍历数组列表,打印出所有员工信息。所以我尝试了这个:

public void retrieveEmployee() {
    for (int i = 0; i < employees.size(); i++) {
        System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary);
    }
}

但这不起作用,因为我无法访问 - 例如 - employee.ID或employee.name。在第二种方法中。 updateEmployee 我本来希望更改信息,但由于可访问性,这不起作用。我感谢任何帮助。这是我的三个班级:

public class Employee {

protected String ID;
protected String name;
protected double grossSalary;
final String END_OF_LINE = System.lineSeparator();

   public Employee (String ID, String name, double grossSalary){
       this.ID = ID;
       this.name = name;
       this.grossSalary = grossSalary;
   }

   public double getGrossSalary() {
       return grossSalary;
   }

   public void setGrossSalary(double grosssalary) {
       this.grossSalary = grossSalary;
   }

   public String getName(){
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}

这是我的主要课程:

public class Main {
private static String END_LINE;
private Scanner sc;
public String name;
public String ID;
public double salary;
private int GPA;
private ReusaxCorp reusaxcorp; 

public Main(){
    sc = new Scanner(System.in);
    END_LINE = System.lineSeparator();
}


   public void presentoptions(){
       while (true){
           System.out.println("=== Welcome === ");
           System.out.println("Choose an option below: ");
           System.out.println(" ");
           System.out.println("1. Register an employee. ");
           System.out.println("2. Remove an employee. ");
           System.out.println("3. Retrieve an employees information. ");

           int option = sc.nextInt();

           switch (option) {
               case 1:
                   System.out.println("What type of employee? " + END_LINE
                        + " - Intern. " + END_LINE
                        + " - Employee. " + END_LINE
                        + " - Manager. " + END_LINE
                        + " - Director." + END_LINE);
                   String type = sc.nextLine();

                   createEmployee();
                   break;

               case 2:
                   break;
               case 3:
                   reusaxcorp.retrieveEmployee();
                   break;
               default:
                   System.out.println("Error. Please try again.");
                   break;
           }
       }
   }

   public void createEmployee(){
       String typeofemployee = sc.nextLine();

           System.out.println("What's the ID of the new " + typeofemployee + "?");
           ID = sc.nextLine();
           System.out.println("What's the name of the new " + typeofemployee + "?");
           name = sc.nextLine();
           System.out.println("What's the salary of the new " + typeofemployee + "?");
           salary = sc.nextDouble();

           Employee employee = new Employee(ID, name, salary);

           switch (typeofemployee) {
               case "Intern":
                   System.out.println("What's the new Interns GPA? ");
                   GPA = sc.nextInt();                   
               case "Employee":
                   break;

               case "Manager":
                   break;

               case "Director":
                   break;

               default:
                   System.out.println("Error");
                   break;
           }
   }

   public static void main(String[] args) {
       Main runcode = new Main();
       runcode.presentoptions();
   }
}

最后是ReusaxCorp班。

public class ReusaxCorp extends Main {

   ArrayList<Employee> employees = new ArrayList<Employee>();
   final String END_OF_LINE = System.lineSeparator();

   public void registerEmployee(){
       employees.add(new Employee(ID, name, salary));
   }

   public void retrieveEmployee() {
       for (int i = 0; i < employees.size(); i++) {
           System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary);
       }
   }

   public void updateEmployee(){
   }
}

2 个答案:

答案 0 :(得分:2)

您正在尝试访问类中不存在的变量employee

public void retrieveEmployee() {
   for (int i = 0; i < employees.size(); i++) {

       //add this line
       Employee employee = employees.get(i);

       System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.salary);
   }
}

您还可以使用for-each循环:

for(Employee employee: employees){
    System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.salary);
}

另请注意,使用ID访问employee.ID等字段仅适用于类位于同一个包中(受保护)的情况。否则,您需要创建一个getter并使用getter访问它们,例如employee.getID()

答案 1 :(得分:0)

在遍历员工列表时使用employee.getId()代替employee.ID,您已经在类成员public时实施了protected个getter方法。对其他班级成员(姓名等)也这样做...... 您可以通过索引(如

)从列表中获取员工,从而在解决方案中执行此操作
System.out.println(employees.get(i).getId());

或者你只需​​要为每个&#34;像

一样循环
for (Employee employee : employees) { 
    System.out.println(employee.getId()); 
}