public class Circle {
private double radius;
public Circle(){ //将半径设为0
this.radius = 0;
}
public Circle(double r ){ //创建Circle对象时将半径初始化为r
this.radius = r;
}
public double getArea(){ //获取圆的面积
return Math.PI * this.radius * this.radius ;
}
public double getPerimeter(){//获取圆的周长
return Math.PI * this.radius * 2 ;
}
/** 将圆的面积 ,周长 */
public void show(){
System.out.println("圆的半径:"+this.radius);
System.out.println("圆的面积:"+this.getArea());
System.out.println("圆的周长:"+this.getPerimeter());
}
}
public class Cylinder extends Circle {
private double hight;
/** 构造时设置圆的高,并调用父类的构造设置半径 */
public Cylinder(double r,double h){
super(r);
this.hight = h;
}
/** 获得体积 */
public double getVolume(){
double area = super.getArea();
return area * hight;
}
/**打印半径,周长,面积,体积*/
public void showVolume(){
super.show();
System.out.println("圆的体积是:"+this.getVolume());
}
}
public class Test {
public static void main(String[] args) {
Cylinder c = new Cylinder(10,10);
c.showVolume();
}
}