簡體   English   中英

從另一個類訪問主類的鏈接列表

[英]Acces linked list in main class from another class java

我有一個在線商店的項目。 主要方法所在的類OnlineStore是啟動程序的地方。 我也有3個類,User,Item和Package,以及一些繼承的類。

我將所有用戶,項目和軟件包存儲在鏈接列表中。 我在main方法內的OnlineStore類中創建了實例,但我希望能夠隨時訪問它們; Item類可以訪問此鏈接列表並刪除已售出的項目。

public class OnlineStore{

    public static void main(String[] args) {
    LinkedList<User> users = new  LinkedList<User>();
    LinkedList<Item> itemsSold =  new  LinkedList<Item>();
    LinkedList<Item> items =  new  LinkedList<Item>(
    LinkedList<Package> packages =  new  LinkedList<Package>();
    }
}

//Then I create some instances of Items Users and call their methods of buying, 
//loging in, etc..

//If a item is buyed:

public class Buyer extends User {
    //ATTRIBUTES
    private String accountNumber;
    private LinkedList<Item> boughtItems;

    //CONSTRUCTOR
    public Buyer(String n, String id, String pass, String a){
        super(n, id, pass);
        accountNumber = a;
    }

    //METHODS
    public void buy(Item i){
        boughtItems.add(i);
        //Here I need to acces the first class and remove from availableItems
        items.remove(i);
    }



}

謝謝你的幫助! 約翰·R

嘗試這樣的事情:

public class OnlineStore{

    public static LinkedList<User> users = new  LinkedList<User>();
    public static LinkedList<Item> itemsSold =  new  LinkedList<Item>();
    public static LinkedList<Item> items =  new  LinkedList<Item>();
    public static LinkedList<Package> packages =  new  LinkedList<Package>();

    public static void main(String[] args) {

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM