From 37d8b453333125dc8c5787f8a771f52b4ea767b7 Mon Sep 17 00:00:00 2001 From: liuyixiao <3520612068@qq.com> Date: Thu, 9 Apr 2026 08:06:51 +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 | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 w5 diff --git a/w5 b/w5 new file mode 100644 index 0000000..5196371 --- /dev/null +++ b/w5 @@ -0,0 +1,47 @@ +// 抽象父类 Vehicle +abstract class Vehicle { + // 抽象方法 run(),没有方法体,强制子类实现 + public abstract void run(); +} + +// 子类 Car +class Car extends Vehicle { + @Override + public void run() { + System.out.println("小汽车在公路上飞驰 🚗"); + } +} + +// 子类 Bike +class Bike extends Vehicle { + @Override + public void run() { + System.out.println("自行车在非机动车道骑行 🚲"); + } +} + +// 子类 Truck +class Truck extends Vehicle { + @Override + public void run() { + System.out.println("卡车在货运道路运输 🚚"); + } +} + +// 测试类 +public class VehicleTest { + public static void main(String[] args) { + System.out.println("\n=== 进阶题测试 ==="); + // 1. 创建 Vehicle 数组,存放不同子类对象(多态数组) + Vehicle[] vehicles = { + new Car(), + new Bike(), + new Truck() + }; + + // 2. 遍历数组,统一调用 run() 方法,自动触发对应子类的实现 + for (Vehicle v : vehicles) { + v.run(); + } + } +} \ No newline at end of file