作者julian9925 (可怜的大学生)
看板java
标题[问题] class之间做到共享array
时间Thu May 20 17:44:32 2010
下面是我的测试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)
1F:推 PsMonkey:上面那篇,就请您自砍吧 05/20 17:47
2F:推 snowlike:设法参考资讯来源,Store(test) or sellItem(test, "veg. 05/21 18:40
3F:→ julian9925:谢谢大大 朋友也是告诉我上述方法 将旧的物件传到新的 05/22 00:50