2.定义一个圆类Circle,该类能根据圆的半径计算圆的面积和周长,同时也提供了修改半径和获取半径值的方法。再定义一个接口G3D,接口中定义一个求三维图形体积的方法getVolume()。然后定义一个圆锥CircularCone类,它是圆类Circle 的子类同时是G3D的实现类,它能根据底圆面积计算圆锥的体积。编写测试类,在类通过接口G3D回调对象,输出半径为10,高为15的圆锥的体积。
时间: 2024-02-09 22:09:58 浏览: 37
好的,我理解了你的问题。以下是代码实现:
```
// Circle类
class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public double getCircumference() {
return 2 * Math.PI * radius;
}
}
// G3D接口
interface G3D {
double getVolume();
}
// CircularCone类
class CircularCone extends Circle implements G3D {
private double height;
public CircularCone(double radius, double height) {
super(radius);
this.height = height;
}
public double getHeight() {
return this.height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getVolume() {
return (1.0 / 3.0) * super.getArea() * height;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
CircularCone cone = new CircularCone(10, 15);
System.out.println("圆锥的体积为:" + cone.getVolume());
}
}
```
输出结果为:
```
圆锥的体积为:1570.7963267948965
```
阅读全文