public class Square {
private int sideLength;
private int area;
public Square(int initialLength) {
sideLength = initialLength;
area = sideLength * sideLength;
}
public int getArea() { return area; }
public void grow() { sideLength = 2 * sideLength; }
}
我不知道什么是错的以及如何解决它。我认为它是因为我将sideLength设置为initialLength并且它基本上是一样的东西?
答案 0 :(得分:1)
在grow
方法中,由于您要更新sideLength
的值,因此您还应该更新area
的值:
public void grow() {
sideLength = 2 * sideLength;
area = sideLength * sideLength;
}