[問題] class之間做到共享array
下面是我的測試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
05/21 18:40, 2F
→
05/22 00:50, , 3F
05/22 00:50, 3F