简体   繁体   English

访问在另一个类中创建的任何对象

[英]Accessing any object created in another class

I´m fairly new to programming and I´ve been stuck on this for a while now so any help to point me in the right direction would be greatly appreciated! 我对编程还很陌生,现在已经坚持了一段时间,因此,向我指出正确方向的任何帮助将不胜感激!

I´m working on the infamous bank account program and having trouble with accessing a particular object from a different class. 我正在开发臭名昭著的银行帐户程序,无法从其他类访问特定对象。 I have two classes, Customer and Banklogic. 我有两个类,Customer和Banklogic。 The Customer class obviously creates customer objects with a social security number (pnr) and a name. 客户类显然会创建具有社会保险号(pnr)和名称的客户对象。 The Banklogic class handles and manipulates these objects with different methods such as adding them to the bank or changing their name. Banklogic类使用不同的方法来处理和操纵这些对象,例如将它们添加到银行或更改其名称。

My problem is that when I´ve created say three customer objects the only one I can access is the last one created. 我的问题是,当我创建三个客户对象时,我只能访问的对象是最后一个创建的对象。 I need to be able to access any object in order to invoke methods on them. 我需要能够访问任何对象以便调用它们上的方法。

This is the constructor of the Customer class: 这是Customer类的构造函数:

public Customer(long pNr, String name) { this.pNr = pNr; this.name = name; }

This is the method to add a new customer to the bank in the BankLogic class (with a few differnt lists): 这是将新客户添加到BankLogic类中的银行的方法(有一些不同的列表):

public boolean addCustomer(String name, long pNr)
{
    if(!pNrList.contains(pNr))                                  
    {
        customer = new Customer(pNr, name);
        customerList.add(customer);
        String client = Long.toString(customer.getpNr()) + "     " + customer.getName();
        kunder.add(client);
        pNrList.add(pNr);
        return true;
    }
    else return false;
}

If I create three objects in a tester class or in the BankLogic class like this: 如果我在测试器类或BankLogic类中创建三个对象,如下所示:

public static void main(String[] args)
{
    BankLogic a = new BankLogic();
    a.addCustomer("JEAN", 66);
    a.addCustomer("JEN", 67);
    a.addCustomer("ANNA", 70);
}

When I try to access one of them with a getter method from the Customer class, the only returned object is the last one created, in this case: 当我尝试使用Customer类中的getter方法访问其中之一时,唯一返回的对象是最后一个创建的对象,在这种情况下:

Anna 70 安娜70

I don´t think there´sa problem with the getter method from Customer class: 我认为Customer类的getter方法没有问题:

public long getpNr() 
{
    return pNr;
}

I´ve tried another getter mehod as well which won´t work either: 我也尝试了另一种吸气方法,该方法也不起作用:

public long getpNr(long pNr) 
{       
    if(this.pNr == pNr)
    {
        return pNr;
    }
    else return -1;
}

The customerList in the BankLogic class is an Arraylist that holds Customer objects but I can´t figure out how to access the different objects from this either (if it is possible that is). BankLogic类中的customerList是一个包含Customer对象的Arraylist,但我也无法弄清楚如何从中访问不同的对象(如果可能的话)。

PS This is my first post here so please forgive any errors or if this has already been answered elsewhere.. Thanks / Johan 附注:这是我在这里的第一篇文章,因此请原谅任何错误,或者如果其他地方已经回答过。.谢谢/约翰

You are creating only single object of class BankLogic and only updating the attributes(name and pnr) of same object by calling the method addCustomer .Update code as below to create 3 different objects : 您仅创建类BankLogic单个对象,并且仅通过调用方法addCustomer .Update代码更新同一对象的属性(名称和pnr),如下所示,以创建3个不同的对象:

BankLogic a = new BankLogic();
    a.addCustomer("JEAN", 66);
BankLogic b = new BankLogic();
    b.addCustomer("JEN", 67);
BankLogic c = new BankLogic();
    c.addCustomer("ANNA", 70);

Editing : Use below code then the size of the list will be 3 and correct name/pnr will be reflected 编辑:使用下面的代码,则列表的大小将为3,并且将显示正确的名称/ pnr

    ArrayList<BankLogic> obj = new ArrayList<BankLogic>();
        BankLogic a = new BankLogic();
        a.addCustomer("JEAN", 66);
        obj.add(a);
        a = new BankLogic();
        a.addCustomer("JEN", 67);
        obj.add(a);
        a = new BankLogic();
        a.addCustomer("ANNA", 70);
        obj.add(a);

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

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