类的抽象与封装

package zuoye;

public class Circle {
 private double Radius;
 public double getRadius() {
  return Radius;
 }
 public void setRadius(double radius) {
  Radius = radius;
 }
 public Circle( ){
  System.out.println("我是圆!");
 }
 public Circle(double  r){
  this.Radius = r;
 }
 public double getArea(double r){
  return Math.PI * r * r;
 }
 public double getPerimeter(double r){
  return 2 * Math.PI * r;
 }
 public void  show( ){
  System.out.println("圆的面积: " + this.getArea(Radius));
  System.out.println("圆的周长: " + this.getPerimeter(Radius));
 }

}

package zuoye;
public class Cylinder extends Circle {
 private double hight;
 
  public Cylinder (double r, double  h ){
   super(r);
   this.hight = h;
  }
  public double getVolume(){
   return Math.PI * this.getRadius() * this.getRadius() * hight;
 
  }
  public void showVolume( ){
   System.out.println("圆柱体的体积:" + this.getVolume());
  }
}

package zuoye;

public class Test {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Circle cc = new Circle();
  cc.show( );
  Cylinder ccc =new Cylinder(5, 3);
  ccc.showVolume();

 }

}

posted on 2018-04-23 20:38  31王鹏  阅读(112)  评论(1编辑  收藏  举报

导航