第9次作业--接口及接口回调

匿名 (未验证) 提交于 2019-12-03 00:14:01

利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

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;     } }

/**创建一个梯形类继承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 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());             }         }     }  }

运行截图:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!