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

    题目描述

    利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

    源代码

    Shape 接口

    package com.tomotoes.probleam.nine;
    
    public interface Shape {
    	double getArea();
    }
    
    

    工厂类

    package com.tomotoes.probleam.nine;
    
    import com.tomotoes.probleam.nine.shapes.*;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.function.Supplier;
    
    public class Factory {
    	private static Scanner scanner = new Scanner(System.in);
    
    	private static double input() {
    		return scanner.nextDouble();
    	}
    
    	private static Map<String, Supplier<Shape>> factory = new HashMap<>();
    
    	static {
    		factory.put("rect", () -> new Rect(input(), input()));
    		factory.put("circle", () -> new Circle(input()));
    		factory.put("square", () -> new Square(input()));
    		factory.put("trapezoid", () -> new Trapezoid(input(), input(), input()));
    		factory.put("triangle", () -> new Triangle(input(), input(), input()));
    	}
    
    	public static Shape getInstance(String type) {
    		return factory.getOrDefault(type, () -> null).get();
    	}
    }
    
    

    主类

    package com.tomotoes.probleam.nine;
    
    import com.tomotoes.probleam.nine.shapes.Column;
    
    import java.util.Objects;
    import java.util.Scanner;
    
    public class App {
    	private static Scanner scanner = new Scanner(System.in);
    
    	public static void init() {
    		System.out.println("矩形类:   输入 rect -> Rect(宽,长)");
    		System.out.println("圆形类:   输入 circle -> Circle(半径)");
    		System.out.println("三角形类: 输入 triangle -> Triangle(底边,高,斜边)");
    		System.out.println("正方形类: 输入 square -> Square(边长)");
    		System.out.println("梯形类: 输入 trapezoid -> Trapezoid(顶边,底边,高)");
    		System.out.printf("输入其他值, 程序退出.
    
    ");
    	}
    
    	public static void main(String[] args) {
    		init();
    		while (true) {
    			System.out.println("请输入类型参数: ");
    			final String input = scanner.next().trim().toLowerCase();
    
    			System.out.println("请输入类所需的构造参数: ");
    			Shape shape = Factory.getInstance(input);
    
    			if (Objects.isNull(shape)) {
    				System.out.println("程序退出.");
    				return;
    			}
    
    			System.out.println("请输入柱体的高: ");
    			final double height = scanner.nextDouble();
    
    			Column column = new Column(height, shape);
    			System.out.printf("柱体的体积为: " + column.getVolume() + "
    
    ");
    		}
    
    	}
    }
    
    

    柱体类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Column {
    	double height;
    	Shape shape;
    
    	public Column(double height, Shape shape) {
    		this.height = height;
    		this.shape = shape;
    	}
    
    	public double getVolume() {
    		return height * shape.getArea();
    	}
    }
    
    

    矩形类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Rect implements Shape {
    	double width;
    	double length;
    
    	public Rect(double width, double length) {
    		this.width = width;
    		this.length = length;
    	}
    
    	@Override
    	public double getArea() {
    		return width * length;
    	}
    }
    
    

    圆形类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Circle implements Shape {
    	double radius;
    
    	public Circle(double radius) {
    		this.radius = radius;
    	}
    
    	@Override
    	public double getArea() {
    		return Math.pow(radius, 2) * Math.PI;
    	}
    }
    
    

    正方形类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Square implements Shape {
    	double length;
    
    	public Square(double length) {
    		this.length = length;
    	}
    
    	@Override
    	public double getArea() {
    		return length * length;
    	}
    }
    
    

    梯形类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Trapezoid implements Shape {
    	double top;
    	double base;
    	double height;
    
    	public Trapezoid(double top, double base, double height) {
    		this.top = top;
    		this.base = base;
    		this.height = height;
    	}
    
    	@Override
    	public double getArea() {
    		return (top + base) * height * 2;
    	}
    }
    
    

    三角形类

    package com.tomotoes.probleam.nine.shapes;
    
    import com.tomotoes.probleam.nine.Shape;
    
    public class Triangle implements Shape {
    	double base;
    	double height;
    	double oblique;
    
    	public Triangle(double base, double height, double oblique) {
    		this.base = base;
    		this.height = height;
    		this.oblique = oblique;
    	}
    
    	@Override
    	public double getArea() {
    		return base * height / 2;
    	}
    }
    
    

    运行截图

  • 相关阅读:
    深入Eureka/Feign/Hystrix原理学习(1)
    mysql截取函数substring_index()和right()用法
    Mybatis映射文件的自动映射与手动映射问题
    MySQL单向加密函数
    Grovvy带参数的闭包
    微信小程序开发编程手记20190303
    IDEA 各版本在线激活(激活码)
    Vue实例:演示input 和 textarea 元素中使用 v-model 实现双向数据绑定
    mybatis异常解决:class path resource [SqlMapConfig.xml] cannot be opened because it does not exist
    【学亮IT手记】MySql行列转换案例
  • 原文地址:https://www.cnblogs.com/jinma/p/11616097.html
Copyright © 2011-2022 走看看