From 2b616b1f2dcb19398e61e01a3f5b7d4212e28c72 Mon Sep 17 00:00:00 2001 From: pangyaxuan Date: Sun, 29 Mar 2026 22:46:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'W4'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W4 | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 W4 diff --git a/W4 b/W4 new file mode 100644 index 0000000..9c8a8c2 --- /dev/null +++ b/W4 @@ -0,0 +1,57 @@ +// 抽象图形类 +abstract class Shape { + public abstract double getArea(); +} + +// 圆形 +class Circle extends Shape { + private double r; + + public Circle(double r) { + this.r = r; + } + + @Override + public double getArea() { + return Math.PI * r * r; + } +} + +// 矩形 +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; + } +} + +// 三角形 +class Triangle extends Shape { + private double base; + private double height; + + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return 0.5 * base * height; + } +} + +// 工具类 +class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("面积:" + shape.getArea()); + } +}