题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
代码及注释:
1 package com.ccut.java; 2 3 /** 4 * 接口,一个抽象方法 5 * 6 * 7 */ 8 public interface Shape { 9 abstract double getArea(); 10 }
1 package com.ccut.java; 2 /** 3 * 圆类继承shape接口 4 * @author Administrator 5 * 6 */ 7 public class Circle implements Shape { 8 final double PAI=3.14; 9 double r; 10 Circle(double r){ 11 this.r=r; 12 } 13 public double getArea() { 14 return PAI*r*r; 15 } 16 17 }
1 package com.ccut.java; 2 3 /** 4 * 梯形类,继承自Shape接口 5 * 6 * @author Administrator 7 * 8 */ 9 public class Ladder implements Shape { 10 11 double upper_base; 12 double under_base; 13 double height; 14 15 Ladder(double upper_base, double under_base, double height) { 16 this.upper_base = upper_base; 17 this.under_base = under_base; 18 this.height = height; 19 } 20 21 public double getArea() { 22 return (upper_base + under_base) * height / 2; 23 } 24 }
1 package com.ccut.java; 2 /** 3 * 矩形类 继承接口,求面积,以及构造方法 4 * @author Administrator 5 * 6 */ 7 public class Rectangle implements Shape { 8 double length; 9 double width; 10 Rectangle(double length,double width){ 11 this.length=length; 12 this.width=width; 13 } 14 15 public double getArea(){ 16 17 return length*width; 18 } 19 }
1 package com.ccut.java; 2 /** 3 * 正方形类继承Shape接口 4 * 5 */ 6 public class Square implements Shape { 7 8 double length; 9 double area; 10 11 Square(double length) { 12 this.length = length; 13 } 14 15 public double getArea() { 16 17 return area = length * length; 18 } 19 }
1 package com.ccut.java; 2 /** 3 * 三角形类 继承自Shape接口 4 * @author Administrator 5 * 6 */ 7 public class Triangle implements Shape { 8 9 double a; 10 double b; 11 double c; 12 13 Triangle(double a, double b, double c) { 14 this.a = a; 15 this.b = b; 16 this.c = c; 17 } 18 19 public double getArea() { 20 double d = (a + b + c) / 2; 21 return Math.sqrt(d * (d - a) * (d - b) * (d - c)); 22 } 23 }
1 package com.ccut.java; 2 /** 3 * 柱体类,一个换底方法 4 * @author Administrator 5 * 6 */ 7 public class Podetium{ 8 9 10 Shape shape; 11 double height; 12 Podetium(Shape shape,double height){ 13 this.shape=shape; 14 this.height=height; 15 } 16 void changeBase(Shape shape) { 17 this.shape=shape; 18 } 19 double getVolume(){ 20 return shape.getArea()*height; 21 } 22 }
1 package com.ccut.java; 2 3 import java.util.Scanner; 4 /** 5 * 工厂类,将计算工厂包装起来 6 * 7 */ 8 public class Factory { 9 Shape shape=null; 10 Scanner reader = new Scanner(System.in); 11 12 public Factory(double height) { 13 Podetium podetium=new Podetium(this.getShape(),height); 14 System.out.println(podetium.getVolume()); 15 } 16 public Shape getShape() { 17 System.out.println("请选择所求柱体的底:(j(矩形)z(正方形)y(圆形)s(三角形)t(梯形)"); 18 char S=reader.next().charAt(0); 19 switch(S) { 20 case'j':System.out.println("矩形柱体的体积是:");shape=new Rectangle(2,3);break; 21 case'z':System.out.println("正方形柱体的体积是:");shape=new Square(7);break; 22 case'y':System.out.println("圆形柱体的体积是:");shape=new Circle(7);break; 23 case's':System.out.println("三角形柱体的体积是:");shape=new Triangle(2,3,4);break; 24 case't':System.out.println("梯形柱体的体积是:");shape=new Ladder(2,3,4);break; 25 } 26 return shape; 27 } 28 }
1 package com.ccut.java; 2 3 /** 4 * 测试类 5 * 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 double height = 6; 10 Factory factory = new Factory(height); 11 } 12 }