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

一、题目:

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

二、代码实现:

cone

package gc;

public class cone {
    Shape shape;
    double h;
    public cone(Shape shape,double h){
        this.shape=shape;
        this.h=h;
    }
     public double getv(){
        return shape.getArea()*h;
    }
     
    }

矩形

package gc;

public class juxing implements Shape {
        double a;
        double b;
        public juxing(double a,double b){
            this.a=a;
            this.b=b;
        }
        public double getArea() {
            return a*b;
        }
}

三角

package gc;

public class sanjiao implements Shape{
    double a;
    double b;
    double c;
    public sanjiao(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));
              
          }
     }

梯形

package gc;

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

package gc;

public class yuanxing implements Shape{
    double r;
    double p=3.14;
    public yuanxing(double r){
        this.r=r;
    }
    public double getArea(){
        return p*r*r;
    }
    }

正方形

package gc;

public class zheng implements Shape{
    double a;
    public zheng(double a){
        this.a=a;
    }
          public double getArea(){
          return a*a;      
     }
    }

梯形

package gc;

public class zhuti{
    Shape shape;
    double high;
    public zhuti(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;
    }
}
package gc;

public interface  Shape {
    double getArea();

}

主方法

package gc;
import java.util.*;
public class zff {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("输入字母:");
        Scanner reader=new Scanner(System.in);
        char c=reader.next().charAt(0);
        factory factory=new factory();
        cone cone=new cone(factory.getShape(c),5);
        System.out.println("柱体的体积是:"+cone.getv());
    }
    }

factory

package gc;

import java.util.Scanner;
 
 public class factory {
    
    Shape getShape(char c){
     Shape shape = null;
 
         switch(c){
         case 'j': shape = new juxing(5,3);break;
         case 'z': shape = new zheng(5);break;
         case 's': shape = new sanjiao(3,4,5);break;
         case 'y': shape = new yuanxing(4);break;
         case 't': shape = new tixing(3,4,3);break;
         }
         
        return shape;
    }
 }

 

三、运行结果:

 

posted @ 2019-10-11 17:51  朱佳美20194662  阅读(154)  评论(0编辑  收藏  举报