第九次作业---接口回调

一、题目

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

/**创界接口Shape 储存一个计算面积对的方法**/
package com;
public interface Shape {
double getArea();
}
/**创建矩形类,实现图形接口的实现,重写计算面积的方法,和构造方法,成员变量长宽**/

package com;
public class Rect implements Shape{
    double wid,len;
    public Rect(double len,double wid){
        this.wid=wid;
        this.len=len;
    }
    
    public double getArea(){
        return wid*len;
    }

}
/**创界圆类,实现图形接口的实现,重写计算面积的方法,和构造方法,成员变量半径**/
package com;
public class Circle implements Shape{
    double r;
    public Circle(double r){
        this.r=r;
    }

    public double getArea(){
        return Math.PI*r*r;
    }

}
/**创建正方形类,实现图形接口的实现,重写计算面积的方法,和构造方法,成员变量边**/
package com;
public class Squre implements Shape{
    double bian;
     Squre(double bian) {
       this.bian=bian;    }
    public double getArea(){
        return bian*bian;
    }
}
/**创建梯形类,实现图形接口的实现,重写计算面积的方法,和构造方法,成员变量上底,下底,高**/

package com;
public class Tixing implements Shape{
    double top,high,di;
    public Tixing(double top,double high,double di){
        this.top=top;
        this.di=di;
        this.high=high;
    }
    public double getArea(){
        return (top+high)*high/2;
    }
}
/**创建三角形类,实现图形接口的实现,重写计算面积的方法,和构造方法,成员变量三边长**/

package com;
public class Trangle implements Shape {
    double a,b,c;
    Trangle(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 com;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Fac {
    Shape shape;
    Scanner rea=new Scanner(System.in);
    
    char ch=rea.next().charAt(0);
    
    public Shape getShape(){
        switch(ch){
        case 's': shape =new Trangle(3,4,5); break;    
        case 'c': shape = new Circle(3); break;
        case 'r': shape = new Rect(3,4); break;
        case 'z': shape = new Squre(4); break;
        case 't': shape = new Tixing(3,4,4); break;
        
        }
        return shape;
    }

    public char getCh() {
        return ch;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    public void setCh(char ch) {
        this.ch = ch;
    }



}
/**创建柱体类,和柱体类得构造方法,写一个计算体积的方法,调图形类的求面积的方法,和换底的方法**/
package com;
public class Cone {
     Shape shape;
     double high;
     Cone(Shape shape ,double high){
         this.shape=shape;
         this.high=high;
     }
    public double getVolume() {
        return shape.getArea()*high;
    }


void  changeDi(Shape shape){
this.shape =shape;       
   }
}
 
 
 
 
/**创建主方法,实例化工厂类和柱体类,输出体积**/
package com;
import java.util.Scanner;
public class Text {
    public static void main(String[] args) {
        for(int i=0;i<5;i++){
        System.out.print("请输入形状");
        Fac fac1=new Fac();
        Cone cone1=new Cone(fac1.getShape(),5);
        cone1.changeDi(fac1.getShape());
        double vol1=cone1.getVolume();
        System.out.println("体积"+vol1);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/chenxiangyuu/p/11612087.html