zoukankan      html  css  js  c++  java
  • 第9次作业--接口及接口回调

    一、题目

           利用接口和接口回调,实现简单工厂模式,当输入不同字符,代表相应图形时,

    利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

    1 package angle;
    2 
    3 public interface shape {
    4     double getArea();
    5 
    6 }
    package angle;
    
    
    public class rectangle implements shape{
        double length;
        double width;
        public rectangle(double length,double width){
            this.length=length;
            this.width=width;
        }
        public double getArea() {
            return length*width;
        }
    
    }
    package angle;
    
    public class triangle implements shape{
        double a;
        double b;
        double c;
        public triangle(double a,double b,double c){
            this.a=a;
            this.b=b;
            this.c=c;
        }
        double p=(a+b+c)/2;
        
        public double getArea() {
            return Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }
    
    }
    package angle;
    
    public class circular implements shape{
        double r;
        public circular(double r){
            this.r=r;
        }
        public double getArea() {
            return 3.14*r*r;
        }
    
    }
    package angle;
    
    import java.util.Scanner;
    
    public class choice {
        
            Scanner reader = new Scanner(System.in);
            shape shape = null;
            public shape choice(char c){
            switch (c) {
            case 'r':
                System.out.println("请输入矩形的长和宽");
                shape=new rectangle(2, 2);
                break;
            case 't':
                System.out.println("请输入三角形的三条边");
                shape = new triangle(4,5,6);
                break;
            case 'c':
                System.out.println("请输入圆的半径");
                shape = new circular(5);
                break;
                }
    
    return shape;
            }
        }
        
    package angle;
    
    import java.util.Scanner;
    
    public class Text {
    
        public static void main(String[] args) {
            System.out.println("请选择需要的图形用图形名称首字母小写来代替");
            Scanner reader=new Scanner(System.in);
            char c = reader.next().charAt(0);
                   choice factory = new choice();
                   cone cone = new cone(factory.choice(c), reader.nextDouble());//获取底面积,高
                    System.out.print(cone.getvolum());
            
    
        }
    
    }
    package angle;
    
    public class cone {
        shape shape;
        double high;
        public cone(shape shape ,double high){
            this.shape=shape;
            this.high=high;
    }
        public double getvolum(){
            return high*shape.getArea();
            
        }
        public void changeDi(){
            this.shape=shape;
        }
    }

  • 相关阅读:
    关于【缓存穿透、缓存击穿、缓存雪崩、热点数据失效】问题的解决方案
    pycharm快捷键
    php 整理的零碎知识点
    phpadmin 导出csv格式的数据处理
    python 单例模式的实现
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
  • 原文地址:https://www.cnblogs.com/guoxiang19/p/11668771.html
Copyright © 2011-2022 走看看