abstract class Shape { public abstract double getArea(); } // 圆形 class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } } // 矩形 class Rectangle extends Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } @Override public double getArea() { return length * width; } } // 三角形 class Triangle extends Shape { private double base; private double height; public Triangle(double base, double height) { this.base = base; this.height = height; } @Override public double getArea() { return 0.5 * base * height; } } // 工具类 class ShapeUtil { public static void printArea(Shape shape) { System.out.println("图形面积:" + shape.getArea()); } }