From 505430da0c9eab5067f5bbc340537cb48cf6d1ee Mon Sep 17 00:00:00 2001 From: Wangyanshu <2680603193@qq.com> Date: Thu, 7 May 2026 15:06:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E9=A2=98'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基础题 | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 基础题 diff --git a/基础题 b/基础题 new file mode 100644 index 0000000..7c4a9a9 --- /dev/null +++ b/基础题 @@ -0,0 +1,39 @@ +// 父类 Shape +public class Shape { + public void draw() { + System.out.println("绘制一个图形"); + } +} + +// 子类 Circle +public class Circle extends Shape { + @Override + public void draw() { + System.out.println("绘制一个圆形 ○"); + } +} + +// 子类 Rectangle +public class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("绘制一个矩形 ▭"); + } +} + +// 测试主类 +public class ShapeTest { + // 题目要求:drawShape 方法,接收 Shape 类型参数 + public static void drawShape(Shape s) { + s.draw(); // 多态调用 + } + + public static void main(String[] args) { + Shape circle = new Circle(); + Shape rect = new Rectangle(); + + System.out.println("=== 基础题测试 ==="); + drawShape(circle); + drawShape(rect); + } +} \ No newline at end of file