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

一、题目

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

二、源程序

1、Shape.java

package hom_11;
/**
 * 创建一个接口Shape,声明getArea()方法
 * */
public interface Shape {
    double getArea();
}

2、Rectangle.java

package hom_11;
/**
 * 创建Rectangle类实现Shape接口,类中包含两个成员变量width,length,一个有参构造方法public Rectangle(double width,double length),重写接口方法getArea()
 * */
public class Rectangle implements Shape{
    double width,length;
    public Rectangle(double width,double length){
        this.width=width;
        this.length=length;
    }
    @Override
    public double getArea(){
        // TODO Auto-generated method stub
        return width*length;
    }

}

3、Square.java

package hom_11;
/**
 * 创建一个Rectangle的子类Square,类中包含Square的有参构造方法,无参构造方法,重写了父类中的getArea()方法
 * */
public class Square extends Rectangle {

    public Square(double side){
        super(side, side);
    }
    public Square(){
        super(0,0);
    }
    public double getArea() {
        return width*width;
        // TODO Auto-generated constructor stub
    }

}

4、Triangle.java

package hom_11;
/**
 * 创建一个三角形类实现接口,类中包含三个成员变量,一个有参构造方法public Triangle(double a,double b,double c),重写接口方法getArea()
 * */
public class Triangle implements Shape{
    double a,b,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));
    }
}

5、Circle.java

package hom_11;
/**
 * 创建Circle类实现接口,类中包含一个成员变量,一个有参构造方法,重写方法getArea()
 * */
public class Circle implements Shape{
    double r;
    public Circle(double r){
        this.r=r;
    }
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return Math.PI*r*r;
    }
    
}

6、Ti.java

package hom_11;
/**
 * 创建一个Ti类实现接口Shape,类中包含三个成员变量,一个有参构造方法,重写接口方法getArea()
 * */
public class Ti implements Shape{
    double a,b,h;
    public Ti(double a,double b,double h){
        this.a=a;
        this.b=b;
        this.h=h;
    }
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return ((a+b)*h)/2;
    }
    
}

7、Cone.java

package hom_11;
/**
 * 创建柱体类Cone,类中包含两个成员变量,一个有参构造方法public Cone(Shape shape,double high),求体积方法getVolume(),换底方法setRect(Shape shape)
 * */
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 setRect(Shape shape){
        this.shape=shape;
    }
}

8、Factory.java

package hom_11;
/**
 * 创建工厂类实现具体的功能,包含一个静态成员变量,一个静态方法。
 * */
public class Factory {
    static Shape shape=null;
    public static Shape shape1(char c){
        switch (c) {
        case 'r':
            System.out.println("矩形为底的柱体体积为:"); 
            shape=new Rectangle(3, 5);break;
        case 's':
            System.out.println("正方形为底的柱体体积为:"); 
            shape=new Square(6);break;    
        case 't':
            System.out.println("三角形为底的柱体体积为:"); 
            shape=new Triangle(3, 4, 5);break;
        case 'c':
            System.out.println("圆形为底的柱体体积为:"); 
            shape=new Circle(3);break;
        case 'i':
            System.out.println("梯形为底的柱体体积为:"); 
            shape=new Ti(5, 6, 9);break;
        
        }
        return shape;
    }
}

9、Test.java

package hom_11;

import java.util.Scanner;
/**
 * 创建Test类实现根据图形求面积的功能,从键盘输入相对应的字符可以求出柱体的体积
 * */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        while(true){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入对应的图形");
        System.out.println("矩形 r  正方形 s  三角形 t  圆形 c  梯形 i  ");
        char c=sc.next().charAt(0);
        Cone cone=new Cone(Factory.shape1(c), 5);
        System.out.println(cone.getVolume());
        }
    }

}

三、运行结果

 

posted @ 2019-10-11 22:01  20194628胡艳春  阅读(300)  评论(0编辑  收藏  举报