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

  • 相关阅读:
    【AtCoder】ARC075
    【BZOJ】3022: [Balkan2012]The Best Teams
    【Codeforces】Gym100633 D. LWDB
    MIME协议在邮件中的应用详解
    struts返回json数据
    mysql-存储过程(转载)
    安卓OKhttp请求封装
    安卓动态添加碎片
    通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
    安卓原生与hml交互(WebView基础)
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4924199.html
Copyright © 2020-2023  润新知