// 定义 Shape 接口interface Shape { // 求面积方法 double getArea(); // 求周长方法 double getPerimeter(); // 显示面积方法 void showArea(); // 显示周长方法 void showPerimeter();}// 定义 Circle 类,实现 Shape 接口class Circle implements Shape { private double radius; // 圆的半径 // 构造函数 public Circle(double r) { radius = r; } // 实现接口中的方法 public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2 * Math.PI * radius; } public void showArea() { System.out.println("圆的面积为:" + getArea()); } public void showPerimeter() { System.out.println("圆的周长为:" + getPerimeter()); }}// 定义 Rectangle 类,实现 Shape 接口class Rectangle implements Shape { private double width; // 矩形的宽 private double height; // 矩形的高 // 构造函数 public Rectangle(double w, double h) { width = w; height = h; } // 实现接口中的方法 public double getArea() { return width * height; } public double getPerimeter() { return 2 * (width + height); } public void showArea() { System.out.println("矩形的面积为:" + getArea()); } public void showPerimeter() { System.out.println("矩形的周长为:" + getPerimeter()); }}// 主类public class Main { public static void main(String[] args) { Circle circle = new Circle(5.0); circle.showArea(); // 显示圆的面积 circle.showPerimeter(); // 显示圆的周长 Rectangle rectangle = new Rectangle(3.0, 4.0); rectangle.showArea(); // 显示矩形的面积 rectangle.showPerimeter(); // 显示矩形的周长 }}
时间: 2024-04-23 17:23:26 浏览: 76
这段代码定义了一个 Shape 接口,其中包含求面积、求周长、显示面积和显示周长的方法。然后定义了 Circle 和 Rectangle 两个实现了 Shape 接口的类,分别表示圆和矩形。在主类中创建了一个 Circle 对象和一个 Rectangle 对象,并分别调用它们的 showArea 和 showPerimeter 方法显示它们的面积和周长。
相关问题
定义一个接口Shape,包含计算面积和计算周长的抽象方法
以下是定义接口Shape的代码示例:
```java
public interface Shape {
// 计算面积的抽象方法
double getArea();
// 计算周长的抽象方法
double getPerimeter();
}
```
在上述代码中,我们定义了一个接口Shape,该接口包含两个抽象方法getArea()和getPerimeter(),分别用于计算形状的面积和周长。由于接口中的方法都是抽象的,因此在实现接口时需要实现这两个方法。
定义一个接口shape,包含计算面积和计算周长的抽象方法
好的,下面是定义一个接口shape,包含计算面积和计算周长的抽象方法的代码示例:
```java
public interface Shape {
double getArea();
double getPerimeter();
}
```
以上代码中,我们定义了一个名为Shape的接口,该接口包含两个抽象方法getArea()和getPerimeter()。这两个方法分别用于计算形状的面积和周长。由于这是一个接口,所以我们只需要定义方法的签名,而不需要提供具体的实现。具体的实现将在实现该接口的类中完成。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)