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

题目:

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

代码:

Shape.java

/**
 * 创建图形接口,声明求面积方法
 */
package b;

/*public abstract class Shape {
     abstract double getArea();
}*/
public interface Shape{
    double getArea();
}

Rectangle.java

/**
 * 矩形类,包含两个成员变量一个构造方法,并重写了getArea方法;
 */
package b;

public class Rectangle implements Shape {
    double width;
    double length;
    public Rectangle(double width,double length){
        this.width=width;
        this.length=length;
    }
    public double getArea(){
        return width*length;
    }
}

Zheng.java

/**
 * 正方形类继承矩形类,创建有参、无参构造方法和求面积方法
 */
package b;

public class Zheng extends Rectangle{
    public Zheng(double width) {
        super(width, width);
        }
    public Zheng(){
        super(0,0);
    }
    public double getArea(){
        return width*width;
    }
    
}

Triangle.java

/**
 * 三角形类,定义成员变量和构造方法和求面积方法
 */
package b;

public class Triangle implements Shape{
    double a;
    double b;
    double c;
    public Triangle(double a,double b,double c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public double getArea(){
        double p=(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
}

Tixing.java

/**
 * 梯形类,包含三个成员变量,两个方法;构造方法和求面积方法
 */
package b;

public class Tixing implements Shape {
    double a;
    double b;
    double h;
    public Tixing(double a,double b,double h){
        this.a=a;
        this.b=b;
        this.h=h;
    }
    public double getArea(){
        return (a+b)*h/2;
    }
}

Circle.java

package b;

public class Circle implements Shape{
         double r;
         public Circle(double r){
             this.r=r;
         }
         public double getArea(){
             return (Math.PI*r*r);
         }
}

Cone.java

/**
 * 柱体类,定义图形对象和高,创建构造方法和求体积方法
 */
package b;

public class Cone {
    Shape shape;
    double high;
    public Cone(Shape shape , double high){
            this.shape = shape;
            this.high = high;
    }
    public double getV(){
       return (shape.getArea()*high);
    }
    public void setD(Shape shape){ //利用修改器换底
       this.shape = shape;
    }
}

Factory.java

/**
 * 工厂类,定义图形对象,创建getShape()方法,方法中写一个开关方法,返回图形对象
 */
package b;

public class Factory {
    Shape shape=null;
   Shape getShape(char c){
       switch(c){
       case'r':shape=new Rectangle(4,5);break;
       case'z':shape=new Zheng(6);break;
       case's':shape=new Triangle(3,4,5);break;
       case't':shape=new Tixing(3,5,7);break;
       case'c':shape=new Circle(5);break;
       default:System.out.println("error");  
       }
       return shape;
   }
   
}

Test.java

package b;
import java.util.*;
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         while(true){                  //创建循环
                Scanner reader=new Scanner(System.in);
                System.out.println("请输入字母:");
                char c=reader.next().charAt(0);
                if(c=='o'){                    //判断是否退出循环
                    System.out.println("程序结束");
                    break;
                }
                Factory f = new Factory();
                Cone cone=new Cone(f.getShape(c),6);//创建柱体对象
                System.out.println("柱体的体积是:"+cone.getV());      //输出结果
         }        
    }
}

运行结果

 

posted @ 2019-10-06 18:52  王庆祥  阅读(104)  评论(0编辑  收藏  举报