用java 1定义接口IShape,该接口有2个方法,方法一(求周长):double perimeter(),方法二(求面积):double area();2自行定义圆类(Circle),实现IShape接口;3自行定义矩形类(Rectagle),实现IShape接口
时间: 2023-12-10 16:41:41 浏览: 81
```java
//定义IShape接口
public interface IShape {
double perimeter(); //求周长方法
double area(); //求面积方法
}
//圆类实现IShape接口
public class Circle implements IShape {
private double radius; //圆的半径
private final double PI = 3.1415926; //π的值
public Circle(double radius) {
this.radius = radius;
}
@Override
public double perimeter() {
return 2 * PI * radius;
}
@Override
public double area() {
return PI * radius * radius;
}
}
//矩形类实现IShape接口
public class Rectangle implements IShape {
private double width; //矩形的宽
private double height; //矩形的高
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double perimeter() {
return 2 * (width + height);
}
@Override
public double area() {
return width * height;
}
}
```
阅读全文