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

一、题目

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

二、代码实现

  1、矩形类

 

 1 /*
 2  * 矩形类接Shape接口
 3  * 定义length,width变量
 4  * getArea求矩形面积
 5  */
 6 public class juxing implements Shape {
 7     double length;
 8     double width;
 9     
10     public juxing(double length, double width){
11         this.length = length ;
12         this.width = width ;
13     }
14     
15     public double getArea(){
16         return length * width ;
17     }
18 }

 

  2、正方形类

 1 /*
 2  正方形子类继承矩形父类
 3  getArea方法求正方形面积
 4  */
 5 public class Zheng extends juxing {
 6     public Zheng(double side) {
 7         super(side,side);
 8     }
 9     
10     public Zheng(){
11         super(0,0);
12     }
13     
14     public double getArea(){
15         return width * width;
16     }
17 }

  3、三角形类

 1 /*
 2  * 三角形类接Shape接口
 3  * 变量a,b,c
 4  * getArea求三角形面积
 5  */
 6 public class Triangle implements Shape {
 7     double a;
 8     double b;
 9     double c;
10     
11     public Triangle(double a, double b, double c){
12         this.a = a;
13         this.b = b;
14         this.c = c;
15     }
16     
17     public double getArea(){
18         double p = (a+b+c) / 2;
19         return Math.sqrt(p*(p-a)*(p-b)*(p-c));
20     }
21 }

  4、圆形类

 1 /*
 2  * circle类接Shape接口
 3  * 变量人r,p赋值3.14
 4  * getArea方法求面积
 5  */
 6 public class Circle implements Shape {
 7     double p = 3.14;
 8     double r;
 9     
10     public Circle(double r){
11         this.r =r;
12     }
13     
14     public double getArea(){
15         return p*r*r;
16     }
17 }

  5、梯形类

 1 /*
 2  * Ti类接Shape接口
 3  * 变量s,x,l
 4  * getArea求梯形面积
 5  */
 6 public class Ti implements Shape {
 7     double s;
 8     double x;
 9     double l;
10     
11     public Ti(double s, double x, double l){
12         this.s = s;
13         this.x = x;
14         this.l = l;
15     }
16     
17     public double getArea(){
18         return (s+x)*l/2;
19     }
20 }

  6、接口

1 /*
2  * Shape接口调用getArea方法
3  */
4 public interface Shape {
5     double getArea();
6 }

  7、求体积

 1 /*
 2  * Cone类创建shape对象,定义变量high
 3  * getVolume求shape接口接的形状的体积
 4  * setRect修改器
 5  */
 6 public class Cone {
 7     Shape shape;
 8     double high;
 9     
10     public Cone(Shape shape, double high){
11         this.shape = shape;
12         this.high = high;
13     }
14     
15     public double getVolume(){
16         return shape.getArea() * high;
17     }
18     
19     public void setRect(Shape shape){
20         this.shape = shape;
21     }
22 }

  8、主方法

 1 /*
 2  * Test类,主方法输入字符,并输出相应图形的体积
 3  */
 4 import java.util.Scanner;
 5 
 6 public class Test {
 7 
 8     public static void main(String[] args) {
 9         
10         System.out.println("输入字符 j:矩形,z:正方形,s:三角形,y:圆形,t:梯形");
11         Scanner reader = new Scanner(System.in);
12         char c = reader.next().charAt(0);
13         
14         Factory factory= new Factory();
15         Cone cone = new Cone(factory.getShape(c), 10);
16         
17         System.out.println(cone.getVolume());
18         
19     }
20 
21 }

  9、简单工厂模式

 1 /*
 2  * Factory类
 3  * getShape方法求输入字符相对应的类的底面积
 4  */
 5 import java.util.Scanner;
 6 
 7 public class Factory {
 8     
 9     Shape getShape(char c){
10     Shape shape = null;
11 
12         switch(c){
13         case 'j': shape = new juxing(5,3);break;
14         case 'z': shape = new Zheng(5);break;
15         case 's': shape = new Triangle(3,4,5);break;
16         case 'y': shape = new Circle(4);break;
17         case 't': shape = new Ti(3,4,3);break;
18         }
19         
20         return shape;
21     }
22 }

   三、运行结果截图

 

 

 

posted on 2019-10-10 22:02  龙卷风摧毁停车场·  阅读(136)  评论(0编辑  收藏  举报