我有一个静态的产品列表类型。当我从 Shopowner 类填充此列表时,它工作正常,但是当我编译 Customer.java 时,列表返回空白集。为什么填充的列表不保留?
class Products {
String name;
int itemcode;
Products(){}
static List <Products> list = new ArrayList<Products>();
Products(String name,int itemcode)
{
this.name=name;
this.itemcode=itemcode;
}
public String toString()
{return (name+""+itemcode);}
}
class Shopowner {
public static void main (String ...at)
{
Products o = new Products("Shamppo",12);
Products.list.add(o);
Products o1 = new Products("choco",1112);
Products.list.add(o1);
System.out.println(Products.list); //prints fine
}
}
class Customer {
public static void main (String args[])
{
System.out.println(Products.list); //prints []
}
}
输出(编译 Customer.jav 时)
[]