第9次作业--接口及接口回调
题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
代码:
Shape.java(接口)
1 package factorial; 2 /** 3 * 4 * @ClassName: Shape 5 * @author Dandelion 6 * @date: 2019年10月7日 上午11:41:54 7 * 面积接口 8 */ 9 public interface Shape { 10 double getArea(); //抽象方法 11 }
RectangleV3.java:
1 package factorial; 2 /** 3 * 4 * @ClassName: RectangleV3 5 * @author Dandelion 6 * @date: 2019年10月7日 上午10:06:17 7 * 矩形底面积 8 * 接口为shape 9 */ 10 public class RectangleV3 implements Shape{ 11 protected double length; 12 protected double width; 13 14 15 //构造方法 16 public RectangleV3() { 17 18 } 19 public RectangleV3(double length, double width){ 20 this.setLength(length); 21 this.setWidth(width); 22 } 23 //修改器和访问器 24 public double getLength() { 25 return length; 26 } 27 public void setLength(double length) { 28 this.length = length; 29 30 } 31 public double getWidth() { 32 return width; 33 } 34 public void setWidth(double width) { 35 this.width = width; 36 } 37 38 39 public double getArea() { 40 return length * width; 41 } 42 43 44 }
Square.java:
1 package factorial; 2 /** 3 * 4 * @ClassName: Square 5 * @author Dandelion 6 * @date: 2019年10月7日 上午10:07:46 7 * 正方形底面积,可继承长方形,间接与接口shape有关系 8 */ 9 public class Square extends RectangleV3 { 10 11 //构造方法 12 public Square() { 13 14 } 15 public Square(double length) { 16 this.length = length; 17 } 18 19 public double getArea(){ 20 return length * length; 21 } 22 23 }
Trapezoid.java:
1 package factorial; 2 /** 3 * 4 * @ClassName: Trapezoid 5 * @author Dandelion 6 * @date: 2019年10月7日 上午11:50:59 7 * 梯形面积 8 */ 9 10 11 public class Trapezoid extends graphSwtich implements Shape{ 12 protected double length; //上底 13 protected double width; //下底 14 protected double high; //高 15 16 //构造方法 17 public Trapezoid(){ 18 19 } 20 public Trapezoid(double length, double width,double high) { 21 this.setLength(length); 22 this.setWidth(width); 23 this.setHigh(high); 24 } 25 26 27 //访问器修改器 28 public double getLength() { 29 return length; 30 } 31 public void setLength(double length) { 32 this.length = length; 33 } 34 public double getWidth() { 35 return width; 36 } 37 public void setWidth(double width) { 38 this.width = width; 39 } 40 public double getHigh() { 41 return high; 42 } 43 public void setHigh(double high) { 44 this.high = high; 45 } 46 47 48 49 public double getArea() { 50 return (length + width) * high /2; 51 } 52 53 54 }
Triangle.java:
package factorial; /** * * @ClassName: Triangle * @author Dandelion * @date: 2019年10月7日 下午2:18:50 * 三角形面积 */ public class Triangle extends Trapezoid{ public Triangle(double length, double width, double high) { this.length = length; this.width = width; this.high = high; } public double getArea(){ double temp1 = (length + width + high) / 2; return Math.sqrt(temp1 * (temp1 - length) * (temp1 - width) * (temp1 - high)); } }
Circle.java:
1 package factorial; 2 /** 3 * 4 * @ClassName: Circle 5 * @author Dandelion 6 * @date: 2019年10月7日 下午4:25:29 7 * 圆形面积 8 */ 9 public class Circle implements Shape{ 10 11 protected double radius; 12 13 //构造方法 14 public Circle() { 15 16 } 17 public Circle(double radius) { 18 this.setRadius(radius); 19 } 20 //访问器与修改器 21 public double getRadius() { 22 return radius; 23 } 24 public void setRadius(double radius) { 25 this.radius = radius; 26 } 27 28 public double getArea() { 29 return Math.PI * radius * radius; 30 } 31 32 }
Volumev3.java:
1 package factorial; 2 /** 3 * 4 * @ClassName: Volumev3 5 * @author Dandelion 6 * @date: 2019年10月7日 上午11:42:26 7 * 体积 8 */ 9 public class Volumev3 { 10 private double high; 11 private double area; 12 13 //构造方法 14 public Volumev3() { 15 16 } 17 //构造方法:长方形 18 public Volumev3(double high ,RectangleV3 area) { 19 this.setHigh(high); 20 this.area = area.getArea(); 21 } 22 //构造方法:正方形 23 public Volumev3(double high ,Square area) { 24 this.setHigh(high); 25 this.area = area.getArea(); 26 } 27 //构造方法:梯形 28 public Volumev3(double high ,Trapezoid area) { 29 this.setHigh(high); 30 this.area = area.getArea(); 31 } 32 //构造方法:三角形 33 public Volumev3(double high ,Triangle area) { 34 this.setHigh(high); 35 this.area = area.getArea(); 36 } 37 //构造方法:圆形 38 public Volumev3(double high ,Circle area) { 39 this.setHigh(high); 40 this.area = area.getArea(); 41 } 42 43 //访问器和修改器 44 public double getHigh() { 45 return high; 46 } 47 48 public void setHigh(double high) { 49 this.high = high; 50 } 51 52 53 54 public double getVolume() { 55 return area * high; 56 } 57 58 public void show() { 59 System.out.println("体积为:"+ this.getVolume()); 60 61 } 62 63 }
graphSwtich.java:
1 package factorial; 2 import java.util.*; 3 /** 4 * 5 * @ClassName: graphSwtich 6 * @author Dandelion 7 * @date: 2019年10月7日 上午11:47:55 8 * 选择分支 9 */ 10 public class graphSwtich { 11 Scanner input = new Scanner(System.in); 12 public void graphSwitch(char capital){ 13 14 switch(capital) { 15 case 'C': 16 case 'c': 17 System.out.println("请输入矩形柱体(长方形柱体)的长和宽和高:"); 18 double Clength = input.nextDouble(); 19 double Cwidth = input.nextDouble(); 20 double Chigh = input.nextDouble(); 21 RectangleV3 C = new RectangleV3(Clength,Cwidth); 22 Volumev3 Cvolume = new Volumev3(Chigh,C); 23 Cvolume.show(); 24 break; 25 26 27 case 'Z': 28 case 'z': 29 System.out.println("请输入矩形柱体(正方形柱体)的长和高:"); 30 double Zlength = input.nextDouble(); 31 double Zhigh = input.nextDouble(); 32 Square Z = new Square(Zlength); 33 Volumev3 Zvolume = new Volumev3(Zhigh,Z); 34 Zvolume.show(); 35 break; 36 37 38 case 'Y': 39 case'y': 40 System.out.println("请输入圆柱体的半径和高:"); 41 double Yradius = input.nextDouble(); 42 double Yhigh = input.nextDouble(); 43 Circle Y = new Circle(Yradius); 44 Volumev3 Yvolume = new Volumev3(Yhigh,Y); 45 Yvolume.show(); 46 break; 47 48 49 case 'T': 50 case 't': 51 case 'S': 52 case 's': 53 if(capital == 'T' || capital == 't' ) 54 System.out.println("请输入矩形柱体(梯形柱体)的各部分:"); 55 else if(capital == 'S' || capital == 's' ) 56 System.out.println("请输入三角形柱体的各部分:"); 57 double Tlength = input.nextDouble(); 58 double Twidth = input.nextDouble(); 59 double Thigh = input.nextDouble(); 60 double Vhigh = input.nextDouble(); 61 62 if(capital == 'T' || capital == 't' ) { 63 Trapezoid T = new Trapezoid(Tlength,Twidth,Thigh); 64 Volumev3 Tvolume = new Volumev3(Vhigh,T); 65 Tvolume.show(); 66 } 67 else if(capital == 'S' || capital == 's' ) { 68 Triangle S= new Triangle(Tlength,Twidth,Thigh); 69 Volumev3 Svolume = new Volumev3(Vhigh,S); 70 Svolume.show(); 71 } 72 break; 73 74 75 default: 76 System.out.println("输入的字符错误,请重新输入!!"); 77 } 78 } 79 80 }
TestV1.java:
1 package factorial; 2 3 import java.util.*; 4 5 public class TestV1 { 6 public static void main(String[] args) { 7 graphSwtich test = new graphSwtich(); 8 Scanner input = new Scanner(System.in); 9 System.out.println("长方形(C/c) \t 正方形(Z/z) \t 梯形(T/t) \t 三角形(S/s) \t 圆形(Y/y)"); 10 while(true) { 11 System.out.println("请输入字符:"); 12 char capital = input.next().charAt(0); 13 test.graphSwitch(capital); 14 } 15 } 16 }
运算结果: