我有三个课程:Main
,ReusaxCorp
和Employee
。让我们直截了当地说:在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(){
}
}
答案 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());
}