题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
/** 创建了一个图形接口 shape ,抽象方法getArea() ,
创建矩形类Rectangle、正方形类Square、圆形类Cricle、三角形类Triangle、梯形类Trapezoid 调用接口,
创建一个柱体类 Cone ,包含一个换底方法和一个求体积的方法 getVolume() ,
创建一个工厂类Factory,利用switch语句选择柱体的底,数据回调给接口Shape,
创建主类 Test ,由用户输入图形字符,输入以相应图形为底的柱体体积 。 */
图形接口 shape:
public interface Shape { double getArea(); }
矩形类Rectangle:
public class Rectangle implements Shape { protected double length; protected double width; public Rectangle(double length,double width) { this.length=length; this.width=width; } Rectangle() { // TODO Auto-generated constructor stub } public double getArea() { // TODO Auto-generated method stub return length*width; } }
正方形类Square:
public class Square implements Shape { double side; Square(double side){ this.side=side; } public double getArea() { return side*side; } }
圆形类Cricle:
public class Cricle implements Shape{ double r; final double PI=3.14; public Cricle(double r) { this.r=r; } public double getArea() { return PI*r*r; } }
三角形类Triangle:
public class Triangle implements Shape{ double tr1; double tr2; double tr3; public Triangle(double tr1,double tr2,double tr3) { this.tr1=tr1; this.tr2=tr2; this.tr3=tr3; } public double getArea() { double p=(tr1+tr2+tr3)/2; return Math.sqrt(p*(p-tr1)*(p-tr2)*(p-tr3)); } }
梯形类Trapezoid:
public class Trapezoid implements Shape { double a; double b; double c; public Trapezoid(double a,double b,double c) { this.a=a; this.b=b; this.c=c; } public double getArea() { return (a+b)*c/2; } }
柱体类 Cone:
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 setShape(Shape shape) { this.shape=shape; } }
工厂类Factory:
import java.util.Scanner; public class Factory { Shape shape = null; Scanner sc=new Scanner(System.in); Shape getShape(char c) { switch (c) { case 'j':System.out.print("请依次输入矩形的长和宽:\n"); double length=sc.nextDouble(); double width=sc.nextDouble(); shape=new Rectangle(length,width); break; case 'z':System.out.print("请输入正方形的边长:\n"); double side=sc.nextDouble(); shape=new Square(side); break; case 'y':System.out.print("请输入圆形的半径:\n"); double r=sc.nextDouble(); shape=new Cricle(r); break; case 's':System.out.print("请输入三角形的三边长:\n"); double tr1=sc.nextDouble(); double tr2=sc.nextDouble(); double tr3=sc.nextDouble(); shape=new Triangle(tr1, tr2, tr3); break; case 't':System.out.print("请依次输入梯形的上底,下底,高:\n"); double a=sc.nextDouble(); double b=sc.nextDouble(); double h=sc.nextDouble(); shape=new Trapezoid(a, b, h); break; } return shape; } }
主类 Test:
import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Factory f=new Factory(); System.out.print("矩形:j;正方形:z;圆形:y;三角形:s;梯形:t"+"\n"+"请输入图形字符:\n"); Scanner sc=new Scanner(System.in); while(true) { char c=sc.next().charAt(0); if(c!='j'&&c!='z'&&c!='y'&&c!='s'&&c!='t') { System.out.print("输入错误,请重新输入\n"); } else { System.out.print("请输入柱体的高:"); double h=sc.nextDouble(); Cone cone=new Cone(f.getShape(c),h); System.out.print("体积为:"+cone.getVolume()); } } } }
运行截图: