博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

第9次作业--接口及接口回调

Posted on 2019-10-05 13:26  王海楠  阅读(156)  评论(0编辑  收藏  举报

一、题目

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

 二、源代码

 Juxing.java

/*
创建Juxing类,实现接口Shape
创建成员变量宽width,长length
创建求矩形面积方法
*/


package
com; public class Juxing implements Shape { double width; double length; public Juxing(double width, double length) { this.width = width; this.length = length; } public double getArea() { //创建求矩形面积的方法 return width * length; } }

Zheng.java

/*
创建Zheng类,继承Juxing类
创建成员变量边长side
求正方形面积
*/

package
com; public class Zheng extends Juxing { public Zheng(double side) { super(side, side); } public Zheng() { super(0, 0); } public double getArea() { //创建求正方形面积方法 return width * width; } }

Triangle.java

/*创建Triangle类,实现Shape接口
求三角形面积
*/

package
com; 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; } public double getArea() { //创建求三角形面积方法 double p = (a + b + c) / 2; return Math.sqrt(p * (p - a) * (p - b) * (p - c)); } }

Tixing.java

/*
创建Tixing类
求梯形面积
*/

package
com; public class Tixing implements Shape { double s; double x; double h; public Tixing(double s, double x, double h) { this.s = s; this.x = x; this.h = h; } public double getArea() { //创建求梯形面积的方法 return (s + x) * h / 2; } }

Yuan.java

/*
创建Yuan类
求圆的面积
*/

package
com; public class Yuan implements Shape { double r; double PI = 3.14; public Yuan(double r, double PI) { this.r = r; } public double getArea() { //创建求圆面积的方法 return PI * r * r; } }

Shape.java

/*
定义接口Shape
*/

package
com; public interface Shape { double getArea(); }

Cone.java

/*创建柱体类
创建Shape对象、柱体高
创建求体积方法
创建换底方法
*/

package
com; public class Cone { Shape shape; double high; public Cone(Shape shape, double high) { this.shape = shape; this.high = high; } public double getVolume() { return shape.getArea() * high; // 接口回调 } public void setRect(Shape shape) { //换底 this.shape = shape; } }

Factory.java

/*创建工厂类
创建方法t
创建输入不同字符代表不同图形类的对象
输入以该图形为底的高
返回Shape
*/


package
com; import java.util.*; public class Factory { static Shape shape = null; public static Shape k(char c) { switch (c) { case 'J': System.out.println("输入矩形为底的柱体高:"); shape = new Juxing(4, 5); break; case 'Z': System.out.println("输入正方形为底的柱体高:"); shape = new Zheng(5); break; case 'T': System.out.println("输入三角形为底的柱体高:"); shape = new Triangle(4, 5, 6); break; case 'C': System.out.println("输入圆形为底的柱体高:"); shape = new Yuan(5, 3.14); break; case 'X': System.out.println("输入梯形为底的柱体高:"); shape = new Tixing(4, 5, 7); break; } return shape; } }

Text.java

/*
创建主方法
插入循环,创建对象,选择不同图形
调用方法求柱体体积
*/


package
com; import java.util.Scanner; import com.Cone; import com.Factory; public class Text { public static void main(String[] args) { while (true) { Scanner reader = new Scanner(System.in); System.out.println("请输入图形字母:"); char c = reader.next().charAt(0); Factory factory = new Factory(); Cone cone = new Cone(factory.k(c), reader.nextDouble()); System.out.println("输出体积为:" + cone.getVolume()); } } }

三、运行结果