From df553a9eed0f8c261c3bbffeae08cc7b6c7d105a Mon Sep 17 00:00:00 2001 From: LiuZihan <1353843969@qq.com> Date: Wed, 1 Apr 2026 22:24:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w4'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/Circle.java | 12 ++++++++++++ w4/Rectangle.java | 14 ++++++++++++++ w4/Shape.java | 3 +++ w4/ShapeTest.java | 18 ++++++++++++++++++ w4/ShapeUtil.java | 5 +++++ 5 files changed, 52 insertions(+) create mode 100644 w4/Circle.java create mode 100644 w4/Rectangle.java create mode 100644 w4/Shape.java create mode 100644 w4/ShapeTest.java create mode 100644 w4/ShapeUtil.java diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..584068d --- /dev/null +++ b/w4/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/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..90cb267 --- /dev/null +++ b/w4/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/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..e1a96c5 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,3 @@ +public abstract class Shape { + public abstract double getArea(); +} \ No newline at end of file diff --git a/w4/ShapeTest.java b/w4/ShapeTest.java new file mode 100644 index 0000000..136a171 --- /dev/null +++ b/w4/ShapeTest.java @@ -0,0 +1,18 @@ +public class ShapeTest { + public static void main(String[] args) { + // 创建各个图形对象 + Shape c = new Circle(5); + Shape r = new Rectangle(4,6); + Shape t = new Triangle(3,4); + + //统一调用工具打印面积 + System.out.print("圆形:"); + ShapeUtil.printArea(c); + + System.out.print("矩形:"); + ShapeUtil.printArea(r); + + System.out.print("三角形:"); + ShapeUtil.printArea(t); + } +} \ No newline at end of file diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..37e32e3 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,5 @@ +public class ShapeUtil { + public static void printArea(Shape shape){ + System.out.println("图形面积:" + shape.getArea()); + } +} \ No newline at end of file