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.

43 lines
1.6 KiB

package com.trae.shape;
/**
* 图形面积计算器测试类
* 用于验证所有形状类和工具类的功能
*/
public class Main {
public static void main(String[] args) {
System.out.println("图形面积计算器测试\n");
// 1. 创建不同形状的实例
Shape circle = new Circle(5.0); // 半径为5的圆
Shape rectangle = new Rectangle(4.0, 6.0); // 长4宽6的矩形
Shape triangle = new Triangle(3.0, 8.0); // 底3高8的三角形
// 2. 使用ShapeUtil统一打印面积
System.out.println("=== 使用ShapeUtil工具类打印面积 ===");
ShapeUtil.printArea(circle);
ShapeUtil.printArea(rectangle);
ShapeUtil.printArea(triangle);
// 3. 演示多态特性
System.out.println("\n=== 演示多态特性 ===");
// 创建形状数组,可以统一处理不同类型的形状
Shape[] shapes = {circle, rectangle, triangle};
for (Shape shape : shapes) {
ShapeUtil.printArea(shape);
}
// 4. 验证属性访问和修改
System.out.println("\n=== 验证属性访问和修改 ===");
Circle myCircle = (Circle) circle;
System.out.println("圆的原始半径: " + myCircle.getRadius());
System.out.println("圆的原始面积: " + myCircle.getArea());
myCircle.setRadius(10.0); // 修改半径
System.out.println("修改后的半径: " + myCircle.getRadius());
System.out.println("修改后的面积: " + myCircle.getArea());
System.out.println("\n=== 测试完成 ===");
}
}