You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
764 B

// 工具类:提供统一处理图形的方法
public class ShapeUtil {
// 统一打印面积的方法:接收任意Shape子类对象
public static void printArea(Shape shape) {
System.out.println("图形面积为:" + shape.getArea());
}
// 测试主方法(可直接运行验证)
public static void main(String[] args) {
// 测试圆形:半径5
Shape circle = new Circle(5);
printArea(circle); // 输出约78.54
// 测试矩形:长4,宽3
Shape rectangle = new Rectangle(4, 3);
printArea(rectangle); // 输出12.0
// 测试三角形:底6,高4
Shape triangle = new Triangle(6, 4);
printArea(triangle); // 输出12.0
}
}