用java语言编写(1)编写 Shape 接口,接口中声明 computeArea 0方法。 (2)定义Rectangle 类和 Circle 类实现 Shape 接口。
时间: 2023-05-23 10:02:59 浏览: 164
(1)
```
public interface Shape {
public double computeArea();
}
```
(2)
```
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double computeArea() {
return length * width;
}
}
public class Circle implements Shape {
private double radius;
private static final double PI = 3.14159;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
return PI * radius * radius;
}
}
```
注释:以上是用 Java 语言编写 Shape 接口,并实现了 Rectangle 类和 Circle 类实现 Shape 接口的代码。Rectangle 类和 Circle 类都实现了 Shape 接口的 computeArea() 方法。Rectangle 类和 Circle 类分别计算矩形和圆形的面积,并返回面积值。
阅读全文
相关推荐


















