zoukankan      html  css  js  c++  java
  • java面向抽象编程样例

    import java.util.*;
     abstract class Geometry{
        public abstract double getArea();
        
    }
     class Pillar{
        Geometry bottom;
        double height;
        Pillar(Geometry bottom ,double height){
            this.bottom=bottom;
            this.height=height;
            
        }
        public double getVolume(){
            return bottom.getArea()*height;
        }
    }

     class Circle extends Geometry{
        double r;
        Circle(double r){
            
            this.r=r;
            
        }
        public double getArea(){
            return(3.14*r*r);
        }
    }

     class Rectangle extends Geometry{
        double a,b;
        Rectangle(double a,double b){
            
            this.a=a;
            this.b=b;
        }
        public double getArea(){
            return(a*b);
        }
    }

    public class Main {
           public static void main(String args[]){
               Pillar pillar;
               Geometry bottom;
               bottom =new Rectangle(12,22);
               pillar=new Pillar(bottom,58);
               System.out.println("矩形的面积"+pillar.getVolume());
               
               bottom=new Circle(10);
               pillar=new Pillar(bottom,58);
               System.out.println("圆柱的体积"+pillar.getVolume());
           }
    }

  • 相关阅读:
    ROS 八叉树地图构建
    2020
    Ubuntu 16.04 配置开机自动挂载 NTFS 机械硬盘!
    从 0 开始机器学习
    Ubuntu Install kitti2bag
    Ubuntu install sublime-text3
    Ubuntu 修复不能访问正确挂载机械硬盘的问题
    Ubuntu 16.04 安装 NVIDIA 显卡驱动!
    ROS 机器人技术
    phpMyAdmin getshell 学习
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4924199.html
Copyright © 2011-2022 走看看