commit a7ac3da214ff891433e9891e90d2b7a6d80e0e76 Author: 故春 <3481369387@qq.com> Date: Fri Mar 27 22:26:02 2026 +0800 添加W3 diff --git a/W3/Circle.java b/W3/Circle.java new file mode 100644 index 0000000..17338cb --- /dev/null +++ b/W3/Circle.java @@ -0,0 +1,12 @@ +public class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} \ No newline at end of file diff --git a/W3/QQ20260327-221432.png b/W3/QQ20260327-221432.png new file mode 100644 index 0000000..3adcd69 Binary files /dev/null and b/W3/QQ20260327-221432.png differ diff --git a/W3/Rectangle.java b/W3/Rectangle.java new file mode 100644 index 0000000..faabd17 --- /dev/null +++ b/W3/Rectangle.java @@ -0,0 +1,14 @@ +public class Rectangle extends Shape { + private double width; + private double height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public double getArea() { + return width * height; + } +} \ No newline at end of file diff --git a/W3/Shape.java b/W3/Shape.java new file mode 100644 index 0000000..390e266 --- /dev/null +++ b/W3/Shape.java @@ -0,0 +1,4 @@ +public abstract class Shape { + // 抽象方法:计算图形面积 + public abstract double getArea(); +} diff --git a/W3/ShapeUtil.java b/W3/ShapeUtil.java new file mode 100644 index 0000000..cbef7bc --- /dev/null +++ b/W3/ShapeUtil.java @@ -0,0 +1,9 @@ +public class ShapeUtil { + public static void printArea(Shape shape) { + if (shape == null) { + System.out.println("图形对象不能为空!"); + return; + } + System.out.printf("图形面积为:%.2f%n", shape.getArea()); + } +} \ No newline at end of file diff --git a/W3/Test.java b/W3/Test.java new file mode 100644 index 0000000..9ce9940 --- /dev/null +++ b/W3/Test.java @@ -0,0 +1,18 @@ +public class Test { + public static void main(String[] args) { + // 创建不同图形对象 + Shape circle = new Circle(3); + Shape rect = new Rectangle(4, 5); + Shape triangle = new Triangle(3, 4, 5); + + // 统一调用工具类打印面积 + System.out.println("圆形:"); + ShapeUtil.printArea(circle); + + System.out.println("矩形:"); + ShapeUtil.printArea(rect); + + System.out.println("三角形:"); + ShapeUtil.printArea(triangle); + } +} \ No newline at end of file diff --git a/W3/Triangle.java b/W3/Triangle.java new file mode 100644 index 0000000..9f9f6a7 --- /dev/null +++ b/W3/Triangle.java @@ -0,0 +1,15 @@ +public class Triangle extends Shape { + private double a, b, c; + + public Triangle(double a, double b, double c) { + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public double getArea() { + double p = (a + b + c) / 2; + return Math.sqrt(p * (p - a) * (p - b) * (p - c)); + } +} \ No newline at end of file diff --git a/W3/W3.txt b/W3/W3.txt new file mode 100644 index 0000000..e69de29 diff --git a/desktop.ini b/desktop.ini new file mode 100644 index 0000000..1511bb0 --- /dev/null +++ b/desktop.ini @@ -0,0 +1,2 @@ +[LocalizedFileNames] +QQ20260327-221432.png=@QQ20260327-221432.png,0