需求是这个样子的:
代码如下
创建Circle 类
package com.hidata.devops.paas.demo;
/**
* 圆
*/
public class Circle {
protected double radius;//半径
public Circle(double r){
this.radius = r;
}
public double getArea(){
return Math.PI * radius *radius;//获取面积
}
}
创建Cylinder类
package com.hidata.devops.paas.demo;
/**
* 任务3、求圆的面积和圆柱体的体积
*/
public class Cylinder extends Circle{
private double height;//高
public Cylinder(double r,double h){
super(r);
this.height = h;
}
public double getVolume(){
return super.getArea()*height;//圆柱体体积
}
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(5.0,10.0);
System.out.println("圆的面积为:" + cylinder.getArea());
System.out.println("圆柱体的体积为:" + cylinder.getVolume());
}
}
运行结果
圆的面积为:78.53981633974483
圆柱体的体积为:785.3981633974483
Process finished with exit code 0