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.
23 lines
636 B
23 lines
636 B
package com.shape;
|
|
|
|
/**
|
|
* Shape 工具类
|
|
* 利用多态统一处理不同图形
|
|
*/
|
|
public class ShapeUtil {
|
|
|
|
/**
|
|
* 打印任意 Shape 子类的面积
|
|
* 体现了多态:同一个方法可以接收不同子类对象
|
|
* @param shape 任意继承自 Shape 的图形对象
|
|
*/
|
|
public static void printArea(Shape shape) {
|
|
if (shape == null) {
|
|
System.out.println("图形对象不能为空!");
|
|
return;
|
|
}
|
|
System.out.printf("%s 的面积是: %.4f%n",
|
|
shape.getClass().getSimpleName(),
|
|
shape.getArea());
|
|
}
|
|
}
|