Circle类
public class Circle {
private double radius;//圆的半径
public Circle(){//radius属性初始化为1
radius = 1.0;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){//求圆的面积
double area = Math.PI * radius * radius;
return area;
}
}
Circle类
public class Cylinder extends Circle{
private double length;
public Cylinder() {//length属性初始化为1
length = 1.0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double findVolume(){//求圆柱的体积
//return Math.PI * getRadius() * getRadius() * length;
return findArea() * length;
}
}
CylinderTest测试类
public class CylinderTest {
public static void main(String[] args) {
Cylinder cylinder = new Cylinder();
cylinder.setRadius(10);
cylinder.setLength(10);
System.out.println("圆柱的体积: " + cylinder.findVolume());
System.out.println("底面圆的面积:" + cylinder.findArea());
}
}