List<Employeee> employees = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String[] input = new String[6];
int n = Integer.valueOf(scanner.nextLine());
for (int i = 0; i < n; i++) {
input = scanner.nextLine().split(" ");
employees.add(new Employeee(input[0], Double.parseDouble(input[1]), input[2], input[3], input[4],
Integer.valueOf(input[5])));
}
for (Object i : employees) {
System.out.println(i.sallary); //And here ofc idk what to do to print them
System.out.println(i.name);
}
所以在这里,我只是从我的自定义类中制作几个对象,然后将它们放在列表中。之后,我用 for 循环遍历该列表,我想打印它们的属性,但它不允许我。我的 Employeee 类很简单,我什至不会粘贴其中的 getter 和 setter。
public class Employeee {
private String name;
private double sallary;
private String possition;
private String department;
private String email;
private int age;
public Employeee(String name, double sallary, String possition, String department, String email, int age) {
this.name = name;
this.sallary = sallary;
this.possition = possition;
this.department = department;
this.email = email;
this.age = age;
}
}
这里有很多问题。
- 您正在拼错您的属性名称,并疯狂放弃。
- 您在 for-each 语句中使用
Object
,您应该在其中使用Employee
。 - 您尝试直接从类外部访问的字段被声明为
private
,这意味着您不能。您应该改用相应的访问器函数。