diff --git a/w5/0d3a3902-d091-4b75-adf0-2bf4c4d77b21.png b/w5/0d3a3902-d091-4b75-adf0-2bf4c4d77b21.png new file mode 100644 index 0000000..2e824c6 Binary files /dev/null and b/w5/0d3a3902-d091-4b75-adf0-2bf4c4d77b21.png differ diff --git a/w5/Main.java b/w5/Main.java new file mode 100644 index 0000000..cc670ba --- /dev/null +++ b/w5/Main.java @@ -0,0 +1,155 @@ +import java.util.Arrays; + +// ========== 1. 抽象类 Shape ========== +abstract class Shape { + protected String name; + + public Shape(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public abstract double getArea(); + + @Override + public String toString() { + return name + " - 面积: " + String.format("%.2f", getArea()); + } +} + +// ========== 2. 圆形类 ========== +class Circle extends Shape { + private double radius; + + public Circle(String name, double radius) { + super(name); + this.radius = radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} + +// ========== 3. 长方形类 ========== +class Rectangle extends Shape { + private double length; + private double width; + + public Rectangle(String name, double length, double width) { + super(name); + this.length = length; + this.width = width; + } + + public double getLength() { + return length; + } + + public void setLength(double length) { + this.length = length; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } + + @Override + public double getArea() { + return length * width; + } +} + +// ========== 4. 三角形类 ========== +class Triangle extends Shape { + private double base; + private double height; + + public Triangle(String name, double base, double height) { + super(name); + this.base = base; + this.height = height; + } + + public double getBase() { + return base; + } + + public void setBase(double base) { + this.base = base; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + @Override + public double getArea() { + return (base * height) / 2; + } +} + +// ========== 5. 工具类 ========== +class ShapeUtil { + + public static void printArea(Shape shape) { + if (shape == null) { + System.out.println("形状不能为空"); + return; + } + System.out.println(shape.toString()); + } + + public static void printAreas(Shape[] shapes) { + System.out.println("=== 形状面积列表 ==="); + for (Shape shape : shapes) { + printArea(shape); + } + System.out.println("=================="); + } +} + +// ========== 6. 主类(必须是public,且与文件名相同)========== +public class Main { + public static void main(String[] args) { + // 创建各种形状 + Circle circle = new Circle("圆形", 5.0); + Rectangle rectangle = new Rectangle("长方形", 4.0, 6.0); + Triangle triangle = new Triangle("三角形", 3.0, 4.0); + + // 使用工具类打印面积 + System.out.println("单个形状面积打印:"); + ShapeUtil.printArea(circle); + ShapeUtil.printArea(rectangle); + ShapeUtil.printArea(triangle); + + // 多态演示:批量处理 + System.out.println("\n批量处理(多态演示):"); + Shape[] shapes = {circle, rectangle, triangle}; + ShapeUtil.printAreas(shapes); + } +} diff --git a/w5/QQ20260329-232941.png b/w5/QQ20260329-232941.png new file mode 100644 index 0000000..2cf0a66 Binary files /dev/null and b/w5/QQ20260329-232941.png differ