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

一、题目

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

 二、源代码

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

三、运行结果

转载于:https://www.cnblogs.com/wanghainan/p/11618667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值