定义一个接口Shape,包含两个方法:计算表面积area()和计算体积volume()定义圆柱体类Cylinder和圆类Circle,其中Circle类是Cylinder类的内部类Circle类中创建成员变量double radius和全参构造方法,创建两个成员方法:计算圆面积public double area()和圆周长public double perimeter();Cylinder类创建两个成员变量:Circle类型的circle和double类型的height,创建全参构造方法,并通过实现Shape接口求圆柱体的表面积和体积创建DemoCylinder测试类,在main方法中先创建Circle内部类对象,然后用该对象创建Cylinder对象并调用方法求半径为5高为10的圆柱体的表面积和体积 public interface Shape { double area(); double volume();} public class Cylinder implements Shape{ Circle circle; double height; static class Circle{ //补充内部类代码 double radius; } //补充外嵌类代码 } public class DemoCylinder { public static void main(String[] args) { //补充测试类代码 }}
时间: 2023-06-13 09:03:00 浏览: 176
代码如下:
```
public interface Shape {
double area();
double volume();
}
public class Cylinder implements Shape{
Circle circle;
double height;
public Cylinder(Circle circle, double height) {
this.circle = circle;
this.height = height;
}
@Override
public double area() {
return 2 * Math.PI * circle.radius * (circle.radius + height);
}
@Override
public double volume() {
return Math.PI * circle.radius * circle.radius * height;
}
static class Circle{
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
public double perimeter() {
return 2 * Math.PI * radius;
}
}
}
public class DemoCylinder {
public static void main(String[] args) {
Cylinder.Circle circle = new Cylinder.Circle(5);
Cylinder cylinder = new Cylinder(circle, 10);
System.out.println("圆柱体的表面积为:" + cylinder.area());
System.out.println("圆柱体的体积为:" + cylinder.volume());
}
}
```
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)