帮我用Java写一个class Rectangle extends GeometricObject
时间: 2023-07-20 07:39:13 浏览: 79
好的,以下是用Java编写的Rectangle类,它继承了GeometricObject类:
```java
public class Rectangle extends GeometricObject {
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);
}
public void printRectangle() {
System.out.println("The rectangle is created " + getDateCreated() +
" and has width " + width + " and height " + height);
}
}
```
在这个Rectangle类中,我们添加了一个width属性和一个height属性,它们分别表示矩形的宽度和高度。我们还添加了一个构造函数,可以根据给定的宽度和高度创建一个矩形。我们还实现了GeometricObject类中的抽象方法getArea()和getPerimeter(),分别计算矩形的面积和周长。最后,我们还添加了一个printRectangle()方法,用于打印矩形的相关信息。
阅读全文