我的任务是创建一个 Square 类,该类扩展了 Rectangle 类,该类覆盖了方法,以便 Square 保持其“方形”(所有边都是平等的)。我已经完成了 add(int, int) 和 add(rectangle) 的方法,但是正在努力覆盖 grow(int, int) 方法。该方法需要覆盖 grow(int, int) 方法,以使 Square 至少增长那么多并且仍然保持其方形。如何修复该方法以使其覆盖 Rectangle 类方法并正常工作?这是我的代码:
public class Square extends Rectangle{
private int x;
private int y;
private int s;
public Square(int x, int y, int s) {
super(x,y,s, s);
this.x=x;
this.y=y;
this.s=s;
}
@Override
public void add(int one, int two) {
super.add(one, two);
super.add(this.getX() +this.getHeight() , this.getY() + this.getWidth() );
}
@Override
public void add(Rectangle rec) {
super.add(rec);
super.add(this.getX() +this.getHeight() , this.getY() + this.getWidth() );
}
@Override
public void grow(int one, int two) {
super.grow(one,two);
this.add(one,two);
}}