利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
/** 创建了一个图形接口 shape ,抽象方法getArea() ,
创建矩形类Rectangle、正方形类Square、圆形类Cricle、三角形类Triangle、梯形类Trapezoid 调用接口,
创建一个柱体类 Cone ,包含一个换底方法和一个求体积的方法 getVolume() ,
创建一个工厂类Factory,利用switch语句选择柱体的底,数据回调给接口Shape,
创建主类 Test ,由用户输入图形字符,输入以相应图形为底的柱体体积 。 */
Shape类(图形接口)
/**创建一个图形类接口和一个求面积的抽象方法*/
public interface shape{
abstract double getArea();
}
Cricle类(圆形类)
/**创建一个圆类的继承接口,并定义半径r,PI,圆的构造方法和重写求面积方法*/
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;
}}
Rectangle类(矩形类)
/** 创建一个矩形类继承shape接口,定义长length,宽width,矩形构造方法和重写求面积方法*/
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类(正方形类)
/**创建一个正方形类继承shape接口,定义bianchang,正方形构造方法和重写求面积方法*/
public class Square implements Shape {
double bianchang;
Square(double bianchang){
this.bianchang=bianchang;
}
public double getArea() {
return bianchang*bianchang;
}
}
Trapezoid(梯形类)
/**创建一个梯形类继承shape,并定义上底a,下底b,高h,梯形构造方法和重写求面积的方法*/
public class Trapezoid implements Shape {
double a;
double b;
double h;
public Trapezoid(double a,double b,double c) {
this.a=a;
this.b=b;
this.h=h;
}
public double getArea() {
return (a+b)*h/2;
}
}
Cone(柱体类)
/**创建一个柱体类,定义柱体的高h,图形shape,换底方法,并求体积方法和构造方法*/
public class Cone {
Shape shape;
double h;
public Cone(Shape shape,double h) {
this.shape=shape;
this.h=h;
}
public double getVolume() {
return shape.getArea()*h;
}
public void setShape(Shape shape) {
this.shape=shape;
}
}
Triangle(三角形类)
/**创建一个三角形类继承shape,定义三边R1,R2,R3,三角形构造方法和重写求面积方法*/
public class Triangle implements shape{
double R1;
double R2;
double R3;
public Triangle(double tr1,double tr2,double tr3) { this.R1=R1; this.R2=R2; this.R3=R3;
} public double getArea() { double p=(R1+R2+R3)/2; return Math.sqrt(p*(p-R1)*(p-R2)*(p-R3)); } }
Factory(工厂类)
/**创建一个工厂类,创建一个图形对象设为空,引用输入操作提示用户输入字符,利用switch语句给柱体赋底*/
import java.util.Scanner;
public class Factory {
shape shape = null;
Scanner sc=new Scanner(System.in);
shape getShape(char c) {
switch (c) {
case 'R':System.out.print("请依次输入矩形的长和宽:\n");
double length=sc.nextDouble();
double width=sc.nextDouble();
shape=new Rectangle(length,width);
break;
case 'S':System.out.print("请输入正方形的边长:\n");
double bianchang=sc.nextDouble();
shape=new Square(bianchang);
break;
case 'C':System.out.print("请输入圆形的半径:\n");
double r=sc.nextDouble();
shape=new Cricle(r);
break;
case 'T':System.out.print("请输入三角形的三边长:\n");
double R=sc.nextDouble();
double R2=sc.nextDouble();
double R3=sc.nextDouble();
shape=new Triangle(R1, R2, R3);
break;
case 'B':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类(主类)
/**提示用户输入,定义高hight,用for循环输出五个图形为底的体积,创建一个工厂类对象,一个柱体类对象,引用换底方法,输出柱体的体积*/
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("矩形:R;正方形:S;圆形:C;三角形:T;梯形:B"+"\n"+"请输入图形字符:\n");
Scanner sc=new Scanner(System.in);
while(true) {
char c=sc.next().charAt(0);
if(c!='R'&&c!='S'&&c!='C'&&c!='T'&&c!='B') {
System.out.print("输入错误,请重新输入\n");
}
else {
System.out.print("请输入柱体的高:");
double hight=sc.nextDouble();
Cone cone=new Cone(f.getshape(c),h);
System.out.print("体积为:"+cone.getVolume());
}
}
}
}
运行截图:

来源:https://www.cnblogs.com/jianghongyan1/p/11669804.html