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
682 B

/**
* 图形工具类:通过多态统一打印任意 {@link Shape} 的面积。
*/
public final class ShapeUtil {
private ShapeUtil() {
// 工具类禁止实例化
}
/**
* 打印给定图形的面积(保留两位小数,便于实验输出阅读)。
*
* @param shape 任意 {@link Shape} 子类实例,可为 null(将给出提示)
*/
public static void printArea(Shape shape) {
if (shape == null) {
System.out.println("图形引用为空,无法计算面积。");
return;
}
System.out.printf("该图形的面积为:%.2f%n", shape.getArea());
}
}