[問題] class之間做到共享array

看板java作者 (可憐的大學生)時間15年前 (2010/05/20 17:44), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/1
下面是我的測試class public class main { public static void main(String[] args) { warehouse test = new warehouse(); test.saveItem("carrot",10); test.saveItem("carrot",10); test.saveItem("apple", 100); test.output(); test.soldItem("apple", 20); test.output(); Store test2 = new Store(); test2.storeItem("carrot",10); test2.storeItem("vegetable",20); test2.sellItem("vegetable",10); test2.output(); } } 這是我的執行結果 ItemBox: carrot 20 apple 100 ItemBox: carrot 20 apple 80 StoreBox: carrot 10 vegetable 10 ItemBox: carrot 20 apple 80 我想問的是在 store sell的vegetable希望能存在warehouse裡 public class warehouse private Item Itembox[] = new Item [10]; 上面是我想存的array public class Store{ private Item Storebox[] = new Item [10]; private int store = 0; private warehouse save = new warehouse(); public void sellItem(String name, int mount) { save.saveItem(name, mount); for(int i = 0 ; i < store; i++) { if(Storebox[i].equal(name)) { Storebox[i].sub(mount); break; } } } } 但是我這樣寫的結果只能在Store中看到vegetable被儲存在array裡 而且Itembox裡只有儲存剛被賣掉的vegetable 但是我的目希望在main.class中看到sell的vegetable存在warehouse.class 的Itembox裡,那我應該如何寫會比較好 謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.121.213.138 ※ 編輯: julian9925 來自: 140.121.213.138 (05/20 17:47)

05/20 17:47, , 1F
上面那篇,就請您自砍吧
05/20 17:47, 1F

05/21 18:40, , 2F
設法參考資訊來源,Store(test) or sellItem(test, "veg.
05/21 18:40, 2F

05/22 00:50, , 3F
謝謝大大 朋友也是告訴我上述方法 將舊的物件傳到新的
05/22 00:50, 3F
文章代碼(AID): #1BzGI2SE (java)