From 7462b67975d30e2eba3d33a2d434a8909bf228e9 Mon Sep 17 00:00:00 2001 From: Zhoutianyu <1941587995@qq.com> Date: Wed, 8 Apr 2026 17:10:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'w5'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w5 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 w5 diff --git a/w5 b/w5 new file mode 100644 index 0000000..c1f0201 --- /dev/null +++ b/w5 @@ -0,0 +1,37 @@ +// 定义Shape类 +abstract class Shape { + // draw方法,由子类重写 + public abstract void draw(); +} + +// Circle子类 +class Circle extends Shape { + @Override + public void draw() { + System.out.println("画一个圆形 ⚪"); + } +} + +// Rectangle子类 +class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("画一个矩形 ▭"); + } +} + +public class Main { + // 编写drawShape方法,接收Shape类型参数 + public static void drawShape(Shape s) { + s.draw(); // 多态调用实际子类的draw方法 + } + + public static void main(String[] args) { + // 测试 + Shape circle = new Circle(); + Shape rectangle = new Rectangle(); + + drawShape(circle); // 输出:画一个圆形 ⚪ + drawShape(rectangle); // 输出:画一个矩形 ▭ + } +} \ No newline at end of file