diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..44eda83 --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,43 @@ +package com.trae.shape; + +/** + * 圆形类 + * 继承自Shape抽象类,实现了getArea()方法 + */ +public class Circle extends Shape { + private double radius; // 圆的半径 + + /** + * 构造方法 + * @param radius 圆的半径 + */ + public Circle(double radius) { + this.radius = radius; + } + + /** + * 获取圆的半径 + * @return 圆的半径 + */ + public double getRadius() { + return radius; + } + + /** + * 设置圆的半径 + * @param radius 圆的半径 + */ + public void setRadius(double radius) { + this.radius = radius; + } + + /** + * 计算圆的面积 + * 面积公式:π * 半径² + * @return 圆的面积 + */ + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} diff --git a/w4/Main.java b/w4/Main.java new file mode 100644 index 0000000..5c5321a --- /dev/null +++ b/w4/Main.java @@ -0,0 +1,43 @@ +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=== 测试完成 ==="); + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..3efe2a5 --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,62 @@ +package com.trae.shape; + +/** + * 矩形类 + * 继承自Shape抽象类,实现了getArea()方法 + */ +public class Rectangle extends Shape { + private double length; // 矩形的长 + private double width; // 矩形的宽 + + /** + * 构造方法 + * @param length 矩形的长 + * @param width 矩形的宽 + */ + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + /** + * 获取矩形的长 + * @return 矩形的长 + */ + public double getLength() { + return length; + } + + /** + * 设置矩形的长 + * @param length 矩形的长 + */ + public void setLength(double length) { + this.length = length; + } + + /** + * 获取矩形的宽 + * @return 矩形的宽 + */ + public double getWidth() { + return width; + } + + /** + * 设置矩形的宽 + * @param width 矩形的宽 + */ + public void setWidth(double width) { + this.width = width; + } + + /** + * 计算矩形的面积 + * 面积公式:长 × 宽 + * @return 矩形的面积 + */ + @Override + public double getArea() { + return length * width; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..78a25f1 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,13 @@ +package com.trae.shape; + +/** + * 形状抽象类 + * 定义了所有形状都必须实现的面积计算方法 + */ +public abstract class Shape { + /** + * 计算形状面积的抽象方法 + * @return 形状的面积 + */ + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..d90099f --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,48 @@ +package com.trae.shape; + +/** + * 形状工具类 + * 提供形状相关的工具方法 + */ +public final class ShapeUtil { + /** + * 私有构造方法,防止实例化 + */ + private ShapeUtil() { + // 工具类不允许实例化 + } + + /** + * 打印形状的面积 + * @param shape 形状对象,可以是Circle、Rectangle或Triangle等任意Shape的子类 + */ + public static void printArea(Shape shape) { + if (shape == null) { + System.out.println("形状对象不能为空"); + return; + } + + String shapeType = getShapeTypeName(shape); + double area = shape.getArea(); + + // 格式化输出,保留两位小数 + System.out.printf("%s的面积是: %.2f\n", shapeType, area); + } + + /** + * 获取形状类型的中文名称 + * @param shape 形状对象 + * @return 形状类型的中文名称 + */ + private static String getShapeTypeName(Shape shape) { + if (shape instanceof Circle) { + return "圆形"; + } else if (shape instanceof Rectangle) { + return "矩形"; + } else if (shape instanceof Triangle) { + return "三角形"; + } else { + return "未知形状"; + } + } +}