• 类的抽象与封装


    //圆类

    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();

     }

    }

  • 相关阅读:
    Gradle 是什么
    Spring AOP知识
    Spring IOC常用注解
    spring 依赖注入
    Java实现基数排序
    Java实现基数排序(解决负数也可以排序)
    2020/4/10安卓开发:Spinner下拉框
    Spring ioc使用
    java实现:归并排序
    centos中docker的安装
  • 原文地址:https://www.cnblogs.com/lx-20163311114/p/8921767.html
Copyright © 2020-2023  润新知