类的抽象与封装

package 圆;
public class circle {
        private double Radius;
        public double getRadius(){
            return Radius;
        };
        public void setRadius(double r) {
             Radius= r;
        }
        public circle(double r){
          Radius=r;
        }
        public double getPerimeter(){//底圆周长
            return 2 * Math.PI * Radius;
        }
        public double getArea(){//面积
            return Math.PI *Radius *Radius;
        }
public void  show( ){
      System.out.println("圆的半径: " +getRadius());
      System.out.println("圆的面积: " +getArea());
      System.out.println("圆的周长: " +getPerimeter());
     }  
}

package 圆;
public class yuanzhu extends circle {//圆柱类继承圆类
    private double height;
    public double getheight() {
        return height;
     }
       public void setheight(double h) {
         height = h;
       }
    public yuanzhu(double r,double h){
        super(r);
         height=h;
    }
    public double getVolume(){//得到体积
    return getArea() * height;
 }
   public void showVolume( ){
       System.out.println("圆柱体的体积:" +getVolume());
      }
}

package 圆;
public class MAIN {
        public static void main(String[] args) {
          circle d1 = new circle(2);//半径初始化为3
          d1.show( );
        yuanzhu d2 =new yuanzhu(3,6);//高初始化为6
          d2.showVolume();
        }
}

posted on 2018-04-24 21:13  三好学生_小标标  阅读(138)  评论(0编辑  收藏  举报