创建一个接口名为Shape,它有一个方法名为getArea(),该方法返回一个double类型的值。在该接口中定义两个常量PI和E,分别设置为3.14159和2.71828。创建两个类Circle和Rectangle来实现该接口,并分别实现getArea()方法以计算圆形和矩形的面积。
时间: 2024-03-17 11:46:43 浏览: 16
好的,以下是针对您的问题的代码示例:
```java
// 定义Shape接口
interface Shape {
double getArea();
double PI = 3.14159;
double E = 2.71828;
}
// 定义Circle类实现Shape接口
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return PI * radius * radius;
}
}
// 定义Rectangle类实现Shape接口
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 getArea() {
return length * width;
}
}
```
以上代码定义了Shape接口,其中包含getArea()方法和常量PI和E。Circle类实现了Shape接口,并计算圆形的面积,Rectangle类也实现了Shape接口,并计算矩形的面积。