public class Pot {
public String shape;
private int capacity;
public ArrayList<Flower> flowers = new ArrayList<Flower>();
public int free_space = capacity - flowers.size(); // why doesn't free_space work?
public Pot(String shape, int capacity) {
this.shape = shape;
this.capacity = capacity;
System.out.println(capacity); // test
System.out.println(flowers.size()); // test
System.out.println(free_space); //test
}
....
}
Run Code Online (Sandbox Code Playgroud)
嗨,为什么我无法获得正确的 free_space?它总是 0。我发现这free_space = capacity - flowers.size()不起作用。
如果我们喜欢:
public static void main(String[] args) {
Pot p = new Pot("square", 8);
Flower f = new Flower(....);
.....
Flower …Run Code Online (Sandbox Code Playgroud)