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.

29 lines
994 B

package com.shape;
public class TestShape {
public static void main(String[] args) {
System.out.println("=== 图形面积计算器(使用抽象类重构)===\n");
// 创建不同图形对象
Circle circle = new Circle(5.0);
Rectangle rectangle = new Rectangle(4.0, 6.0);
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
// 使用 ShapeUtil 统一处理(多态体现)
ShapeUtil.printArea(circle);
ShapeUtil.printArea(rectangle);
ShapeUtil.printArea(triangle);
System.out.println("\n=== 使用 display() 方法 ===");
circle.display();
rectangle.display();
triangle.display();
// 多态数组示例(进一步展示统一处理优势)
System.out.println("\n=== 多态数组演示 ===");
Shape[] shapes = {circle, rectangle, triangle};
for (Shape s : shapes) {
ShapeUtil.printArea(s);
}
}
}