简体   繁体   English

为什么我不能从另一个 class 访问对象的属性? (即使 Object 被传递给试图访问属性的 class)

[英]Why can't I access the Object's properties from another class? (even when the Object was passed to the class that is trying to access the properties)

I hope you are able to help me.我希望你能帮助我。

My question is:我的问题是:

When passing an instance of an object that is in an arraylist, and I pass that arraylist to another class, how do I access it's atributes?当传递 arraylist 中的 object 的实例时,我将 arraylist 传递给另一个 class,如何访问它的属性?

I can access the attributes before passing it to another class.我可以在将其传递给另一个 class 之前访问这些属性。

The "Main" class, passes on the data, to the "Employees" class. “主要”class 将数据传递给“员工”class。

Employees employees = new Employees();
employees.addEmployee("Orlando", "Silva", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getDayShift());
employees.addEmployee("Rui", "Guilherme", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getNightShift());
employees.addEmployee("Marco", "Alberto", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getNightShift());

The "Employees" class receives the data, adds it to an array, and from that array goes to the "AllStaff" class “Employees”class 接收数据,将其添加到数组中,然后从该数组转到“AllStaff”class

Notice, that I have access to the atributes, in the method "addToAllStaff()"请注意,我可以在方法“addToAllStaff()”中访问属性

public class Employees {
    // Atributes
    public String name;
    private String lName;
    private int nID;
    private String address;
    private int phNum;
    private long nSocialSecNum;
    private double minimumWage = 740.83;
    private double employeeWage;
    private String dayShift = "Day shift", afternoonShift = "Afternoon shift", nightShift = "Night shift";
    private String shift;

    private ArrayList<Employees> employeesArrayList = new ArrayList<Employees>();
    private AllStaff allStaff = new AllStaff();
    //---------------------
    // Constructors
    public Employees(){

    }

    public Employees(String name, String lName, int nID, String address, int phNum, long nSocialSecNum, double minimumWage, String shift){
        this.name = name;
        this.lName = lName;
        this.nID = nID;
        this.address = address;
        this.phNum = phNum;
        this.nSocialSecNum = nSocialSecNum;
        this.employeeWage = minimumWage;
        this.shift = shift;
        //----------------
        extraWage();
    }

    //---------------------

    public void addEmployee(String name, String lName, int nID, String address, int phNum, long nSocialSecNum, double minimumWage, String shift){
           Employees employee = new Employees(name, lName, nID, address, phNum, nSocialSecNum, minimumWage, shift);
           employeesArrayList.add(employee);
           addToAllStaff();
    }

    void addToAllStaff(){
        System.out.println("(Class Employees) employees size: " + employeesArrayList.size());

        for (int i = 0; i < employeesArrayList.size(); i++){
            System.out.println("Employee names: " + employeesArrayList.get(i).getName());
            System.out.println("Employee names: " + employeesArrayList.get(i).name);
        }

        allStaff.addEmployees(employeesArrayList);
    }
}

In the class "AllStaff", is where I don't have access to the attributes在 class“AllStaff”中,是我无权访问属性的地方

public class AllStaff {
    static ArrayList <AllStaff> employeesArrayList;

    public AllStaff(){

    }

    public void addEmployees(ArrayList listOfEmployees){
        System.out.println("List of employees size: " + listOfEmployees.size());

        for (int i = 0; i < listOfEmployees.size(); i++){
            System.out.println("Employee names: " + listOfEmployees.get(i).getName());
            System.out.println("Employee names: " + listOfEmployees.get(i).name);
        }

        this.employeesArrayList = listOfEmployees;
    }

For the whole code, please visit this link: https://github.com/OrlandoVSilva/test-to-github/tree/master/test-to-github/src/mercado完整代码请访问此链接: https://github.com/OrlandoVSilva/test-to-github/tree/master/test-to-github/src/mercado

I hope this question has everything you need.我希望这个问题有你需要的一切。 Thanks谢谢

When using a List (or any object that can be generic ), it's a good practice to specify the type of List you're dealing with.使用 List(或任何 object 可以是通用的)时,最好指定您正在处理的 List 的类型。 In your case, the method addEmployees(ArrayList listOfEmployees) takes in parameter an ArrayList, which could be an ArrayList of literally any object. It could be an ArrayList of Employees, Strings, Integers, whereas you just want it to be an ArrayList of Employees.在您的情况下,方法addEmployees(ArrayList listOfEmployees)接受参数 ArrayList,它可以是字面上任何 object 的 ArrayList。它可以是员工、字符串、整数的 ArrayList,而您只希望它是 882812 的 8905 的 8821992051088 .

the solution is to change your ArrayList into a ArrayList<Employees> listOfEmployees解决方案是将您的 ArrayList 更改为ArrayList<Employees> listOfEmployees

public void addEmployees(ArrayList<Employees> listOfEmployees){
    System.out.println("List of employees size: " + listOfEmployees.size());

    for (int i = 0; i < listOfEmployees.size(); i++){
        System.out.println("Employee names: " + listOfEmployees.get(i).getName());
        //The above should work just fine :)
        System.out.println("Employee names: " + listOfEmployees.get(i).name); 
        //This one will work too as the "name" attribute is public but as you have a getName() method, you probably should make it private :D.
    }

    this.employeesArrayList = listOfEmployees;
}

To explain it quickly without too much details, when you're using a List object, you must specify the List's type by putting <> around it.为了快速解释它而不是太多细节,当您使用列表 object 时,您必须通过将<>放在它周围来指定列表的类型。 If you don't, the default type is Object which is the parent of every types in java.如果不这样做,则默认类型为Object ,它是 java 中每个类型的父级。

Also, i guess your static ArrayList <AllStaff> employeesArrayList;另外,我猜你的static ArrayList <AllStaff> employeesArrayList; is meant to be your list of employees.是你的员工名单。 So replacing it with static ArrayList <Employees> employeesArrayList;所以将其替换为static ArrayList <Employees> employeesArrayList; seems to be what you want to do:D似乎是你想做的:D

You missed generics type in AllStuffs addEmployees() method argument.您在 AllStuffs addEmployees() 方法参数中错过了 generics 类型。 That is the problem.那就是问题所在。 It should be like this:它应该是这样的:

public void addEmployees(ArrayList<Employees> listOfEmployees){

暂无
暂无

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

相关问题 我如何从另一个类访问对象的方法,而该类的可见性不高? - How can I access an object's method from another class giving the class not to much visibility? Minecraft Bukkit:如何从主类访问另一个类的属性/方法? - Minecraft Bukkit: How can I access properties/methods in another class from the main class? 我如何从另一个 class 访问 MainActivity object - How can i access a MainActivity object from another class 为什么我不能从另一个类访问方法 - why i can't access to method from another class 为什么我不能从另一个 class 访问此方法? - Why can't I access this method from another class? 我可以使用单个对象访问具有diff属性的不同类的方法吗? - Can I access methods of different class having diff properties using single object? 如何从 application.properties 访问属性到另一个自定义类? - How to access the properties from application.properties to another custom class? 当它是另一个类的成员时,如何使用 @SuperBuilder 和 @Data 来访问子类属性? - How can I use @SuperBuilder and @Data to be able to access a subclasses properties when it is a member of another class? 无法在 Java 中访问子类中的父类对象属性 - not able to access parent class object properties in child class in Java i wanna access an object of class 1 from class 2 the **object** is from another class lets say class 3 - i wanna access an object of class 1 from class 2 the **object** is from another class lets say class 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM