定义一个圆类,类名Circle,在类Circle中定义数据成员r(圆的半径),定义成员方法getZhou( ),返回圆的周长;定义成员方法getArea( ),返回圆的面积。 由Circle类派生子类Cylinder(圆柱体类),在类Cylinder中定义数据成员h(圆柱体的高),定义成员方法getFaceArea()得到圆柱体的表面积(表面积=getZhou( )*h);定义成员方法getVolume( ),得到圆柱体的体积(体积=getArea( )*h)。 定义主类,类名mainClass,在类中定义main方法,测试以上几个成员方法。
时间: 2024-03-19 09:43:54 浏览: 30
好的,以下是代码实现:
```java
class Circle {
private double r;
public Circle(double r) {
this.r = r;
}
public double getZhou() {
return 2 * Math.PI * r;
}
public double getArea() {
return Math.PI * r * r;
}
}
class Cylinder extends Circle {
private double h;
public Cylinder(double r, double h) {
super(r);
this.h = h;
}
public double getFaceArea() {
return getZhou() * h;
}
public double getVolume() {
return getArea() * h;
}
}
public class mainClass {
public static void main(String[] args) {
Circle c = new Circle(2.0);
System.out.println("圆的周长为:" + c.getZhou());
System.out.println("圆的面积为:" + c.getArea());
Cylinder cy = new Cylinder(2.0, 3.0);
System.out.println("圆柱体的表面积为:" + cy.getFaceArea());
System.out.println("圆柱体的体积为:" + cy.getVolume());
}
}
```
运行结果:
```
圆的周长为:12.566370614359172
圆的面积为:12.566370614359172
圆柱体的表面积为:37.69911184307752
圆柱体的体积为:75.39822368615503
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)