2. 改写实验三中的矩形类,使之实现Printable接口,用printItMyWay()方法将矩形的相关信息(长、宽、周长、面积)打印在屏幕上;改写实验四中的正方形类,重载printItMyWay()方法将正方形的相关信息(边长、周长、面积)打印在屏幕上。
时间: 2024-05-12 14:20:06 浏览: 74
实验三中的矩形类改写如下:
```java
public class Rectangle implements Shape, Printable {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getPerimeter() {
return 2 * (length + width);
}
@Override
public double getArea() {
return length * width;
}
@Override
public void printItMyWay() {
System.out.println("长:" + length + ",宽:" + width + ",周长:" + getPerimeter() + ",面积:" + getArea());
}
}
```
实验四中的正方形类重载printItMyWay()方法如下:
```java
public class Square extends Rectangle {
public Square(double side) {
super(side, side);
}
@Override
public void printItMyWay() {
System.out.println("边长:" + super.length + ",周长:" + getPerimeter() + ",面积:" + getArea());
}
}
```
阅读全文