From 7cbffd25cdff918082a0b455170fc78ce04bff23 Mon Sep 17 00:00:00 2001 From: Mengxinyao Date: Sun, 5 Apr 2026 00:34:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(w4):W4-=E5=AD=9F=E9=91=AB=E5=9E=9A-2025060?= =?UTF-8?q?10204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/Shape | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 w4/Shape diff --git a/w4/Shape b/w4/Shape new file mode 100644 index 0000000..6698569 --- /dev/null +++ b/w4/Shape @@ -0,0 +1,38 @@ +// Shape 类(父类) +abstract class Shape { + public abstract void draw(); // 抽象方法,子类必须实现 +} + +// Circle 类(继承 Shape) +class Circle extends Shape { + @Override + public void draw() { + System.out.println("绘制圆形"); + } +} + +// Rectangle 类(继承 Shape) +class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("绘制矩形"); + } +} + +// 测试类 +public class TestShape { + // 方法:接收 Shape 对象并调用其 draw 方法 + public static void drawShape(Shape s) { + s.draw(); + } + + public static void main(String[] args) { + // 创建不同形状对象 + Shape circle = new Circle(); + Shape rectangle = new Rectangle(); + + // 调用 drawShape 方法测试多态 + drawShape(circle); // 输出:绘制圆形 + drawShape(rectangle); // 输出:绘制矩形 + } +} \ No newline at end of file