[英]Cant access object properties java
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);
}
So here i just make a couple of object from my custom class and i put them inside of the list.所以在这里我只是从我的自定义类中创建了几个对象,然后将它们放在列表中。 And after that i iterate over that list with a for loop and there i want to print their properties, but it doesn't let me.
之后,我用 for 循环遍历该列表,然后我想打印它们的属性,但它不允许我这样做。 My Employeee class is simple I wont even paste the getters and setters from it.
我的 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;
}
}
There are numerous problems here.这里有很多问题。
Object
in the for-each statement where you should be using Employee
.Employee
的 for-each 语句中使用Object
。private
, which means you can't.private
,这意味着您不能。 You should be using the respective accessor functions instead.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.