• 类的抽象与封装


    //圆类

    package lx;

    public class Circle {
     private double Radius;//存放圆的半径
     public double getRadius() {//获取圆的半径
      return Radius;
     }
     public void setRadius(double radius) {//设定半径
      Radius = radius;
     }
     public Circle( ){//无参构造函数,将半径设为0
      this.Radius=0;
     }
     public Circle(double  r){//带参构造函数,将半径初始化为r
      this.Radius = r;
     }
     public double getArea(double r){//获取圆的面积
      return 3.14159 * r * r;
     }
     public double getPerimeter(double r){//获取圆的周长
      return 2 * 3.14159* r;
     }
     public void  show( ){
      System.out.println("圆的面积: " + this.getArea(Radius));
      System.out.println("圆的周长: " + this.getPerimeter(Radius));
     }

    }

    //圆柱类
    public class Cylinder extends Circle {//在圆的基础上定义一个圆柱
     private double hight;
       public Cylinder (double r, double  h ){
       super(r);
       this.hight = h;
      }
      public double getVolume(){//求圆柱的体积
       return 3.14159 * this.getRadius() * this.getRadius() * hight;
      }  
      public void showVolume( ){
       System.out.println("圆柱体的体积:" + this.getVolume());
      }
    }
    public class Test {
      public static void main(String[] args) {
      Circle cc = new Circle();
      cc.show( );
      Cylinder ccc =new Cylinder(3, 6);//圆柱的底面圆的半径和和圆柱的高
      ccc.showVolume();

     }

    }

  • 相关阅读:
    16.检查是否为BST
    15.输出单层结点
    14.高度最小的BST
    *13.有向路径检查
    12.二叉树平衡检查
    11.双栈排序
    10.回文链表
    9.链式A+B
    8.链表分割
    7.访问单个节点的删除
  • 原文地址:https://www.cnblogs.com/zw98916/p/8933446.html
Copyright © 2020-2023  润新知