zoukankan      html  css  js  c++  java
  • 类的抽象与封装

    实验结果

    package 类的抽象与封装;

    public class yuan {//定义构造函数
     
       private double Radius;//成员变量
       public double circle() {
        return Radius;
       }
       public void circle(double r) {//创建时将半径初始化为r
        Radius = r;
       }
       public yuan( ){
        System.out.println("圆!");
       }
       public yuan(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 类的抽象与封装;
    public class yuanzhu extends yuan {
     
       
     private double hight;
        
          public yuanzhu (double r, double  h )
          {
       
           super(r);//成员变量
           this.hight = h;
          }
          public double getVolume(){//get方法存取对象 跟set相比 它不能重载 方法名必须不相同
           return Math.PI * this.circle() * this.circle() * hight;
          
          } 
          public void showVolume( ){
           System.out.println("圆柱体的体积:" + this.getVolume());
          }
        }

    package 类的抽象与封装;
    import java.util.Scanner;//输入函数的要定义的
    public class 主函数 {
     
     public static void main(String[] args){
      double r,h;
      Scanner in=new Scanner(System.in);
      System.out.print("输入圆的半径");
      r=in.nextInt();
      yuan c1=new yuan(r);
      c1.show( );
      Scanner i=new Scanner(System.in);
      System.out.print("输入圆柱的高");
      h=i.nextInt();
      yuanzhu c2=new yuanzhu(r,h);
      c2.showVolume( );
     }

    }

      

     

     

  • 相关阅读:
    蓝瓶的钙,好喝的钙——windows,我要蓝屏的
    gz文件最后四位检测
    中国Linux开源镜像站大全
    linux强制将数据写入磁盘,防止丢失内存的数据
    文件是否真的写入了磁盘?
    OpenStack之日志
    使用 Nmon 监控 Linux 的系统性能
    Android获取系统cpu信息,内存,版本,电量等信息
    Android——service重启
    Android——显示当前运行所有服务,判断服务是否运行
  • 原文地址:https://www.cnblogs.com/infinite14/p/8922332.html
Copyright © 2011-2022 走看看