定义一个接口computable,该接口含有两个方法computeArea和computeLong。 定义实现该接口的类Circle,其中circle类包含一个成员变量,即半径radius,此外该类还要重写computeArea方法计算圆的面积,重写computeLong方法计算圆的周长。
时间: 2024-03-07 16:48:27 浏览: 149
好的,下面是一个示例代码:
```java
interface Computable {
double computeArea();
double computeLong();
}
class Circle implements Computable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double computeArea() {
return Math.PI * radius * radius;
}
public double computeLong() {
return 2 * Math.PI * radius;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("Area: " + circle.computeArea());
System.out.println("Long: " + circle.computeLong());
}
}
```
这个程序定义了一个接口 `Computable`,并且有一个实现了 `Computable` 接口的类 `Circle`,它通过实现 `computeArea()` 和 `computeLong()` 方法来计算圆的面积和周长。在 `Main` 类中,我们创建了一个 `Circle` 对象,并且使用它的方法来计算圆的面积和周长。
阅读全文
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)