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

题目:

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

图形Shape接口:

package com.ccut.hou;
/**
* 创建形状Shape接口
* @author ASUS
*
*/
public interface Shape {
   abstract double getArea();
}

圆形类Circle实现形状Shape接口

package com.ccut.hou;
/**
 * 定义圆形类Circle实现形状Shape接口
 * @author ASUS
 *
 */
public class Circle implements Shape {
    final double PAI=3.14;
    double r;
    Circle(double r){
        this.r=r;
    }
    public double getArea() {
        return PAI*r*r;
    }

}

定义柱体类

package com.ccut.hou;
/**
 * 定义柱体类
 * @author ASUS
 *
 */
public class Cylinder{
Shape shape;
double height;
Cylinder(Shape shape,double height){
    this.shape=shape;
    this.height=height;
}
void changeBase(Shape shape) {
    this.shape=shape;
}
double getVolume(){
    return shape.getArea()*height;
}
}

长方形类Rec实现形状Shape的接口

package com.ccut.hou;
/**
 * 创建长方形类Rec实现形状Shape的接口
 * @author ASUS
 *
 */
public class Rec implements Shape {
    double length;
    double width;
    Rec(double length,double width){
        this.length=length;
        this.width=width;
    }

        public double getArea(){
        
            return length*width;
        }
}

正方形Squ实现形状Shape接口

package com.ccut.hou;
/**
 * 创建正方形Squ实现形状Shape接口
 * @author ASUS
 *
 */
public class Squ implements Shape {

    double length;
    double area;

    Squ(double length) {
        this.length = length;
    }

    public double getArea() {

        return area = length * length;
    }
}

梯形类Trap实现形状Shape接口

package com.ccut.hou;
/**
 * 定义梯形类Trap实现形状Shape接口
 * @author ASUS
 *
 */
public class Trap implements Shape {

    double upper_base;
    double under_base;
    double height;

    Trap(double upper_base, double under_base, double height) {
        this.upper_base = upper_base;
        this.under_base = under_base;
        this.height = height;
    }

    public double getArea() {
        return (upper_base + under_base) * height / 2;
    }
}

三角形类Tri实现形状Shape接口

package com.ccut.hou;
/**
 * 定义三角形类Tri实现形状Shape接口
 * @author ASUS
 *
 */
public class Tri implements Shape {

    double a;
    double b;
    double c;

    Tri(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getArea() {
        double d = (a + b + c) / 2;
        return Math.sqrt(d * (d - a) * (d - b) * (d - c));
    }
}

工厂类

package com.ccut.hou;
import java.util.Scanner;
/**
 * 创建工厂类
 * @author ASUS
 *
 */
public class Factory {
    Shape shape=null;
    Scanner reader = new Scanner(System.in);
    
    public Factory(double height) {
    	Cylinder podetium=new Cylinder(this.getShape(),height);
        System.out.println(podetium.getVolume());
    }
public Shape getShape() {
    System.out.println("****请您按照以下提示操作****");
    System.out.println("~键盘输入 1 即为求矩形的体积;");
    System.out.println("~键盘输入 2 即为求正方形的体积;");
    System.out.println("~键盘输入 3 即为求圆形的体积;");
    System.out.println("~键盘输入 4 即为求三角形的体积;");
    System.out.println("~键盘输入 5 即为求梯形的体积;");
    char S=reader.next().charAt(0);
        switch(S) {
        case '1':System.out.println("矩形柱体的体积是:");shape=new Rec(3,4);break;
        case '2':System.out.println("正方形柱体的体积是:");shape=new Squ(6);break;
        case '3':System.out.println("圆形柱体的体积是:");shape=new Circle(6);break;
        case '4':System.out.println("三角形柱体的体积是:");shape=new Tri(3,5,6);break;
        case '5':System.out.println("梯形柱体的体积是:");shape=new Trap(3,5,6);break;
        }
        return shape;
}
}

主方法MAIN

package com.ccut.hou;
/**
 * 创建主方法
 * @author ASUS
 *
 */
public class Main {
    public static void main(String[] args) {
        double height = 6;
        Factory factory = new Factory(height);
    }
}

运行截图

posted @ 2019-10-10 20:06  侯英龙20194669  阅读(146)  评论(0编辑  收藏  举报