题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
代码:
1、Test.java
1 /** 2 * 测试类,包含一个主方法; 3 * 调用求体积方法输出; 4 */ 5 package cn.edu.ccut.object3; 6 7 import java.util.Scanner; 8 public class Test { 9 10 public static void main(String[] args) { 11 char c; 12 while(true){ 13 System.out.println("请输入柱体底的形状(‘j‘矩形、‘s‘三角形、‘z‘正方形、‘t‘梯形、‘y‘圆形、‘F‘结束程序):"); 14 Scanner reader = new Scanner(System.in); 15 c = reader.next().charAt(0); //c接受字符型输入; 16 if(c == ‘F‘){ //退出程序标志; 17 System.out.println("程序结束!"); 18 break; 19 } 20 factory f = new factory(); 21 Cone cone = new Cone(f.ctrl_di(c) , 5); //定义矩形对象,根据用户输出控制底,高为5; 22 System.out.println("柱体体积是:"+cone.getV()); 23 } 24 } 25 26 }
2、factory.java
1 /** 2 * 工厂类,实现简单工厂模式,包含一个方法; 3 * ctrl_di实现了根据用户输入换底; 4 */ 5 package cn.edu.ccut.object3; 6 7 public class factory { 8 Shape shape = null; 9 public Shape ctrl_di(char c){ //实现了根据用户输入换底; 10 switch(c){ 11 case ‘j‘:shape = new Rec(3,4);break; 12 case ‘s‘:shape = new Triangle(3,4,5);break; 13 case ‘z‘:shape = new Zheng(2);break; 14 case ‘t‘:shape = new Trapezoid(4,6,7);break; 15 case ‘y‘:shape = new Circle(3);break; 16 default:System.out.println("error"); 17 } 18 return shape; 19 } 20 }
3、Shape.java
1 /** 2 * 图形接口; 3 * 定义了一个计算面积的抽象方法getArea; 4 */ 5 package cn.edu.ccut.object3; 6 7 public interface Shape { 8 public double getArea(); //抽象方法计算面积; 9 }
4、Rec.java
1 /** 2 * 矩形类,使用了图形接口,包含两个成员变量一个构造方法,并重写了getArea方法; 3 * 两个成员变量分别代表举行的长和宽; 4 * 构造方法初始化,getArea计算面积; 5 */ 6 package cn.edu.ccut.object3; 7 8 public class Rec implements Shape { 9 double a; 10 double b; 11 Rec(double a , double b){ 12 this.a = a; 13 this.b = b; 14 } 15 public double getArea(){ 16 return (a*b); 17 } 18 }
5、Zheng.java
1 /** 2 * 正方形类,继承了矩形类,包含两个方法; 3 * 构造方法初始化,getArea计算面积 4 */ 5 package cn.edu.ccut.object3; 6 7 public class Zheng extends Rec { 8 Zheng(double a){ 9 super(a, a); //利用super调用父类构造方法; 10 } 11 12 public double getArea(){ //方法重写; 13 return (a*a); 14 } 15 }
6、Circle.java
1 /** 2 * 圆类,使用了图形接口,包含一个成员变量,两个方法; 3 * 成员变量r为半径; 4 * 构造方法初始化,重写了getArea方法; 5 */ 6 package cn.edu.ccut.object3; 7 8 public class Circle implements Shape{ 9 double r; 10 11 public Circle(double r){ 12 this.r = r; 13 } 14 public double getArea(){ 15 return (Math.PI*r*r); 16 } 17 }
7、Triangle.java
1 /** 2 * 三角形类,使用了图形接口,包含了三个成员变量,两个方法; 3 * 成员变量a,b,c分别代表三角形的三个边; 4 * 构造方法初始化,重写了getArea方法; 5 */ 6 package cn.edu.ccut.object3; 7 8 public class Triangle implements Shape{ 9 double a; 10 double b; 11 double c; 12 13 public Triangle (double a , double b , double c){ 14 this.a = a; 15 this.b = b; 16 this.c = c; 17 } 18 public double getArea(){ 19 double p = (a + b + c) / 2; 20 return Math.sqrt(p * (p - a) * (p - b) * (p - c)); 21 } 22 }
8、Trapezoid.java
1 /** 2 * 梯形类,使用了图形接口,包含三个成员变量,两个方法; 3 * 成员变量a,b代表梯形的上底和下底,high代表高; 4 * 构造方法,重写getArea; 5 */ 6 package cn.edu.ccut.object3; 7 8 public class Trapezoid implements Shape{ 9 double a; 10 double b; 11 double high; 12 13 public Trapezoid(double a , double b , double high){ 14 this.a = a; 15 this.b = b; 16 this.high = high; 17 } 18 public double getArea(){ 19 return (((a + b) * high) / 2); 20 } 21 }
9、Cone.java
1 /** 2 * 柱体类,两个成员变量,三个方法; 3 * 成员变量shape代表底,high代表高; 4 * 构造方法初始化,getV计算柱体体积,setDi可用于换底; 5 */ 6 package cn.edu.ccut.object3; 7 8 public class Cone { 9 Shape shape; 10 double high; 11 public Cone(Shape shape , double high){ 12 this.shape = shape; 13 this.high = high; 14 } 15 public double getV(){ 16 return (shape.getArea()*high); 17 } 18 public void setDi(Shape shape){ //利用修改器换底 19 this.shape = shape; 20 } 21 }
运行结果:
原文地址:https://www.cnblogs.com/chris-wang/p/11620316.html
时间: 2024-10-06 05:46:00