3.第二单元任务五实训:通过程序设计几何图形接口(Shape),实现类矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。
public class ShapeTest {
publicstatic void print(Shape shape){
System.out.println(shape.getClass()+":"+"面积:"+shape.area());
System.out.println(shape.getClass()+":"+"周长:"+shape.perimeter());
}
publicstatic void main(String[] args) {
Shapeshape=new Rectangle(4,5);
print(shape);
shape=newCircle(4);
print(shape);
shape=newSquare(5);
print(shape);
}
}
public interface Shape {
public double area();