diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..e175c22 --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,27 @@ +package com.shape; + +public class Circle extends Shape { + private double radius; // 半径 + + public Circle(double radius) { + if (radius <= 0) { + throw new IllegalArgumentException("半径必须大于0"); + } + this.radius = radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + if (radius > 0) { + this.radius = radius; + } + } + + @Override + public double getArea() { + return Math.PI * radius * radius; // πr² + } +} \ No newline at end of file diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..070bfa4 --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,35 @@ +package com.shape; + +public class Rectangle extends Shape { + private double width; // 宽度 + private double height; // 高度 + + public Rectangle(double width, double height) { + if (width <= 0 || height <= 0) { + throw new IllegalArgumentException("长和宽必须大于0"); + } + this.width = width; + this.height = height; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + if (width > 0) this.width = width; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + if (height > 0) this.height = height; + } + + @Override + public double getArea() { + return width * height; // 长 × 宽 + } +} \ No newline at end of file diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..7fc4019 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,22 @@ +package com.shape; + +/** + * 抽象类 Shape + * 提供统一的面积计算接口 + */ +public abstract class Shape { + + /** + * 抽象方法:计算图形面积 + * 子类必须实现该方法 + * @return 图形的面积(double类型) + */ + public abstract double getArea(); + + /** + * 通用方法:打印图形信息(可选,方便扩展) + */ + public void display() { + System.out.println(this.getClass().getSimpleName() + " 的面积为: " + getArea()); + } +} \ No newline at end of file diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..b82cd55 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,23 @@ +package com.shape; + +/** + * Shape 工具类 + * 利用多态统一处理不同图形 + */ +public class ShapeUtil { + + /** + * 打印任意 Shape 子类的面积 + * 体现了多态:同一个方法可以接收不同子类对象 + * @param shape 任意继承自 Shape 的图形对象 + */ + public static void printArea(Shape shape) { + if (shape == null) { + System.out.println("图形对象不能为空!"); + return; + } + System.out.printf("%s 的面积是: %.4f%n", + shape.getClass().getSimpleName(), + shape.getArea()); + } +} \ No newline at end of file diff --git a/w4/TestShape.java b/w4/TestShape.java new file mode 100644 index 0000000..f59aee6 --- /dev/null +++ b/w4/TestShape.java @@ -0,0 +1,29 @@ +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); + } + } +} \ No newline at end of file