简体   繁体   English

无法访问对象属性java

[英]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.这里有很多问题。

  1. You are mis-spelling your attribute names with wild abandon.你疯狂地拼错了你的属性名称。
  2. You are using Object in the for-each statement where you should be using Employee .您在应该使用Employee的 for-each 语句中使用Object
  3. The fields you are trying to access directly from outside the class are declared as private , which means you can't.您尝试直接从类外部访问的字段被声明为private ,这意味着您不能。 You should be using the respective accessor functions instead.您应该改用相应的访问器功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM