要求: 1. 类circle表示一个圆,包含属性radius表示半径,方法getArea()返回圆的面积。 2. 类cylinder表示一个圆柱体,继承自circle类,包含属性height表示高度,方法getVolume()返回圆柱体的体积。 3. 类main包含主函数,创建一个圆柱体对象并输出其面积和体积。 代码如下: ``` // circle类 class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } } // cylinder类 class Cylinder extends Circle { private double height; public Cylinder(double radius, double height) { super(radius); this.height = height; } public double getVolume() { return super.getArea() * height; } } // main类 public class Main { public static void main(String[] args) { Cylinder cylinder = new Cylinder(2., 5.); System.out.println("圆柱体的面积为:" + cylinder.getArea()); System.out.println("圆柱体的体积为:" + cylinder.getVolume()); } } ``` 运行结果: ``` 圆柱体的面积为:12.566370614359172 圆柱体的体积为:62.83185307179586 ```
时间: 2024-03-17 19:44:41 浏览: 24
这个问题和之前的问题是一样的,你可能复制粘贴了两次。我已经回答过了,给你的示例代码如下:
```java
// circle类
class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
// cylinder类
class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getVolume() {
return super.getArea() * height;
}
}
// main类
public class Main {
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(2., 5.);
System.out.println("圆柱体的面积为:" + cylinder.getArea());
System.out.println("圆柱体的体积为:" + cylinder.getVolume());
}
}
```
运行结果:
```
圆柱体的面积为:12.566370614359172
圆柱体的体积为:62.83185307179586
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)