第9次作业--接口及接口回调
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。 Shape类(图形接口) public interface shape{ abstract double getArea(); } Cricle类(圆形类) /**创建一个圆类的继承接口,并定义半径r,PI,圆的构造方法和重写求面积方法*/ public class Cricle implements Shape { double r; final double PI = 3.14; public Cricle(double r) { this.r=r; } public double getArea() { return PI*r*r; }} Rectangle类(矩形类) /** 创建一个矩形类继承shape接口,定义长length,宽width,矩形构造方法和重写求面积方法*/ public class Rectangle implements Shape { protected double length; protected double width; public Rectangle(double length,double width) { this.length=length; this.width=width; } Rectangle()