java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积
网课地址:https://www.bilibili.com/video/BV1CJ411m7gg?p=57
练习要求如上
Circle类
public class Circle {
double radius;
Circle() {
radius = 1;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return 3.14*radius*radius;
}
}
Cylinder类
public class Cylinder extends Circle {
double length;
public Cylinder() {
this.length=1;
}
//获取圆柱的高
public double getLength() {
return length;
}
//设置圆柱的高
public void setLength(double length) {
this.length = length;
}
//返回和计算圆柱的体积
public double getVolume() {
return this.getArea()*this.length;
}
}
TestCylinder
public class TestCylinder {
public static void main(String[] args) {
Cylinder cylinder = new Cylinder();
//设置圆的半径和圆柱的高都为2
cylinder.setRadius(2);
cylinder.setLength(2);
cylinder.getRadius();
cylinder.getLength();
System.out.println("圆的面积:"+cylinder.getArea());
System.out.println("圆柱的体积:"+cylinder.getVolume());
}
}
运行结果