/** * 演示:多态 —— 同一 {@link ShapeUtil#printArea(Shape)} 处理圆、矩形、三角形。 * 运行:javac *.java 后执行 java ShapeCalculatorDemo */ public class ShapeCalculatorDemo { public static void main(String[] args) { Shape circle = new Circle(3.0); Shape rectangle = new Rectangle(4.0, 5.0); Shape triangle = new Triangle(6.0, 4.0); System.out.println("—— 圆 ——"); ShapeUtil.printArea(circle); System.out.println("—— 矩形 ——"); ShapeUtil.printArea(rectangle); System.out.println("—— 三角形 ——"); ShapeUtil.printArea(triangle); System.out.println("—— 多态数组统一处理 ——"); Shape[] shapes = {circle, rectangle, triangle}; for (Shape s : shapes) { ShapeUtil.printArea(s); } } }