改写Rectengle方法,继承GeometricObject 类并实现Comparable接口
时间: 2024-03-24 22:37:31 浏览: 61
java,Comparable接口实例
好的,我理解您的问题了。以下是改写Rectangle方法,继承GeometricObject类并实现Comparable接口的代码示例:
```java
public class Rectangle extends GeometricObject implements Comparable<Rectangle> {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public int compareTo(Rectangle o) {
if (getArea() > o.getArea()) {
return 1;
} else if (getArea() < o.getArea()) {
return -1;
} else {
return 0;
}
}
}
```
在这里,我们扩展了GeometricObject类,并实现了Comparable接口,以便我们可以比较Rectangle对象的大小。我们还重写了compareTo方法,以便我们可以比较Rectangle对象的面积。
阅读全文