第9次作业--接口及接口回调
# 题目
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
# 源代码
## IShape 接口
package com.edu.question.nine.iface; public interface IShape { double getArea(); }
## 工厂类
package com.edu.question.nine.Volume; import java.util.Scanner; import com.edu.question.nine.iface.IShape; import com.edu.question.nine.impl.*; /** * 简单工厂模式 - 选择形状并实例化 * @author 喵 * @date 2019年10月4日下午3:47:12 */ public class Factory { public static IShape shape (String str) { Scanner input = new Scanner(System.in); System.out.print("请输入是参数:"); if (str.equals("circle")) { return new Circle(input.nextDouble()); }else if (str.equals("triangle")) { return new Triangle(input.nextDouble(), input.nextDouble(), input.nextDouble()); }else if (str.equals("square")) { return new Square(input.nextDouble()); }else if (str.equals("rectangle")) { return new Rectangle(input.nextDouble(), input.nextDouble()); }else if (str.equals("trapezoid")) { return new Trapezoid(input.nextDouble(), input.nextDouble(), input.nextDouble()); } return null; } }
## 主类
package com.edu.question.nine.Volume; import java.util.Scanner; import com.edu.question.nine.iface.IShape; /** * 测试 * @author 喵 * @date 2019年10月4日下午4:14:02 */ public class VolumeTest { /** 输出文本*/ public static void printS() { System.out.println("请对应输入:"); System.out.println("圆形: - circle - (半径)"); System.out.println("长方形: - triangle - (长,宽)"); System.out.println("正方形: - square - (边长)"); System.out.println("三角形: - rectangle - (三边)"); System.out.println("梯形: - trapezoid - (上底,下底,高)"); System.out.println("任意输入退出程序"); System.out.print("请输入:"); } public static void main(String[] args) { Scanner input = new Scanner(System.in); printS(); IShape shape = Factory.shape(input.next()); System.out.print("请输入高;"); Prism volume = new Prism(input.nextDouble()); volume.getVolume(shape); } }
## 圆形类
package com.edu.question.nine.impl; import com.edu.question.nine.iface.IShape; /** * 实现圆形 * @author 喵 * @date 2019年10月4日下午4:25:16 */ public class Circle implements IShape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return (Math.PI * Math.pow(radius, 2)); } }
## 三角形类
package com.edu.question.nine.impl; import com.edu.question.nine.iface.IShape; /** * 实现三角形 * @author 喵 * @date 2019年10月4日下午4:25:47 */ public class Triangle implements IShape { private double a; private double b; private double c; public Triangle(double a, double b, double c) { super(); this.a = a; this.b = b; this.c = c; } @Override public double getArea() { double p = (a + b + c) / 2; return (Math.sqrt(p * (p - a) * (p - b) * (p - c))); } }
## 正方形类
package com.edu.question.nine.impl; import com.edu.question.nine.iface.IShape; /** * 实现正方形 * @author 喵 * @date 2019年10月4日下午4:26:01 */ public class Square implements IShape { private double side; public Square(double side) { super(); this.side = side; } @Override public double getArea() { return side * side; } }
## 矩形类
package com.edu.question.nine.impl; import com.edu.question.nine.iface.IShape; /** * 实现矩形 * @author 喵 * @date 2019年10月4日下午4:26:13 */ public class Rectangle implements IShape{ private double weigth; private double longth; public Rectangle(double longth, double weigth) { this.longth = longth; this.weigth = weigth; } @Override public double getArea() { return longth * weigth; } }
## 梯形类
package com.edu.question.nine.impl; import com.edu.question.nine.iface.IShape; /** * 实现梯形 * @author 喵 * @date 2019年10月4日下午4:26:22 */ public class Trapezoid implements IShape { private double upside; private double downside; private double higth; public Trapezoid(double upside, double downside, double higth) { super(); this.upside = upside; this.downside = downside; this.higth = higth; } @Override public double getArea() { return ((upside + downside) * higth) / 2; } }
## 柱体类
package com.edu.question.nine.Volume; import com.edu.question.nine.iface.IShape; /** * 柱体类 * @author 喵 * @date 2019年10月4日下午4:33:24 */ public class Prism { private double hight; public Prism(double hight) { this.hight = hight; } public void getVolume(IShape shape) { System.out.println("体积为:" + (shape.getArea() * hight)); } }

浙公网安备 33010602011771号