根据下图实现类。在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。
Circle (圆) |
-radius :double |
Circle(): 构造方法,将radius属性初始化为1 +setRadius(double radius) : void +getRadius(): double +findArea():double 计算圆的面积 |
Cylinder (圆柱) |
-length:double |
Cylinder(): 构造方法,将length属性初始化为2 +setLength(double length):void +getLength():double +findVolume() :double 计算圆柱体积 |
圆的面积:
public class Circle {
public double radius;
public Circle(){radius=1;}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double findArea(){
double s=radius*radius*3.14;
System.out.println("圆的面积:"+s);
return s;
}
}
圆柱的体积:
public class Cylinder extends Circle{
public double length;
public Cylinder(){length=2;}
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return length;
}
public void findVolume(double s){
double volume=s*length;
System.out.println("圆柱体积:"+volume);
}
public static void main(String[] args) {
Cylinder cylinder=new Cylinder();
double s=cylinder.findArea();
cylinder.findVolume(s);
}
}