Browse Source

fix: 修复练习题中员工数据表格渲染问题——Markdown表格替换为Java代码块(员工绩效管理系统示例数据)

WanJL 23 hours ago
parent
commit
9670705d6b

+ 20 - 0
授课代码/c260724/src/exercise/exercise02/Car.java

@@ -0,0 +1,20 @@
+package exercise.exercise02;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Car
+ * @description
+ * @create 2026/7/24
+ */
+public class Car extends Vehicle{
+    String name = "小汽车";
+
+    @Override
+    public void run() {
+        System.out.println("小汽车在高速行驶...");
+    }
+    public void openSunroof(){
+        System.out.println("打开天窗~");
+    }
+}

+ 21 - 0
授课代码/c260724/src/exercise/exercise02/Vehicle.java

@@ -0,0 +1,21 @@
+package exercise.exercise02;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Vehicle
+ * @description
+ * @create 2026/7/24
+ */
+public class Vehicle {
+    String name = "交通工具";
+    public void run(){
+        System.out.println("交通工具在行驶...");
+    }
+    public void stop(){
+        System.out.println("交通工具刹车停止");
+    }
+    public static void info(){
+        System.out.println("Vehicle 静态方法");
+    }
+}

+ 30 - 0
授课代码/c260724/src/exercise/exercise02/VehicleTest.java

@@ -0,0 +1,30 @@
+package exercise.exercise02;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title VehicleTest
+ * @description
+ * @create 2026/7/24
+ */
+public class VehicleTest {
+    public static void main(String[] args) {
+        // 情况一:父类引用指向子类对象(多态)
+        Vehicle v = new Car();
+        System.out.println(v.name);   // 输出什么?
+        v.run();                      // 输出什么?
+        v.stop();                     // 输出什么?
+        // v.openSunroof();           // 能否调用?编译能通过吗?
+
+        // 情况二:子类引用指向子类对象(普通)
+        Car c = new Car();
+        System.out.println(c.name);   // 输出什么?
+        c.run();                      // 输出什么?
+        c.openSunroof();              // 能否调用?
+
+        // 情况三:静态方法
+        v.info();                     // 调用谁的静态方法?
+        c.info();                     // 调用谁的静态方法?
+        Vehicle.info();               // 推荐哪种调用方式?
+    }
+}

+ 69 - 0
授课代码/c260724/src/exercise/exercise02/题目.md

@@ -0,0 +1,69 @@
+## 练习 2:多态成员访问特点——理解「编译看左边,运行看右边」
+
+**难度**:⭐⭐  
+**知识点**:多态中成员变量和成员方法的访问特点、编译期 vs 运行期
+
+通过代码验证多态的成员访问规则。
+
+**`交通工具` 体系**:
+
+**`Vehicle` 类**(父类):
+
+- **成员变量**:`String name = "交通工具"`
+- **普通方法**:`void run()` —— 输出 `"交通工具在行驶..."`
+- **普通方法**:`void stop()` —— 输出 `"交通工具刹车停止"`
+- **静态方法**:`static void info()` —— 输出 `"Vehicle 静态方法"`
+
+**`Car` 类**(继承 Vehicle):
+
+- **成员变量**:`String name = "小汽车"`
+- **重写** `run()` —— 输出 `"小汽车在高速行驶..."`
+- **特有方法**:`void openSunroof()` —— 输出 `"打开天窗~"`
+
+**测试类要求**:
+
+编写 `VehicleTest` 类,创建一个 `Car` 对象,分别使用「父类引用指向子类对象」和「子类引用指向子类对象」两种方式,观察成员变量、成员方法、特有方法的访问差异。
+
+**模板代码**:
+
+```java
+public class VehicleTest {
+    public static void main(String[] args) {
+        // 情况一:父类引用指向子类对象(多态)
+        Vehicle v = new Car();
+        System.out.println(v.name);   // 输出什么?
+        v.run();                      // 输出什么?
+        v.stop();                     // 输出什么?
+        // v.openSunroof();           // 能否调用?编译能通过吗?
+        
+        // 情况二:子类引用指向子类对象(普通)
+        Car c = new Car();
+        System.out.println(c.name);   // 输出什么?
+        c.run();                      // 输出什么?
+        c.openSunroof();              // 能否调用?
+        
+        // 情况三:静态方法
+        v.info();                     // 调用谁的静态方法?
+        c.info();                     // 调用谁的静态方法?
+        Vehicle.info();               // 推荐哪种调用方式?
+    }
+}
+```
+
+**请写出你的预期输出**(先思考,再运行验证):
+
+```
+v.name → __________
+v.run() → __________
+v.stop() → __________
+c.name → __________
+c.run() → __________
+v.info() → __________
+```
+
+**总结填空**:
+
+1. 成员变量:编译看______,运行看______
+2. 成员方法(非静态):编译看______,运行看______
+3. 静态方法:编译看______,运行看______(因为静态方法属于______,不参与多态)
+4. 通过父类引用,______(能/不能)调用子类的特有方法

+ 17 - 0
授课代码/c260724/src/exercise/exercise03/Animal.java

@@ -0,0 +1,17 @@
+package exercise.exercise03;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Animal
+ * @description
+ * @create 2026/7/24
+ */
+public class Animal {
+    public void eat(){
+        System.out.println("动物在吃东西");
+    }
+    public void sleep(){
+        System.out.println("动物在睡觉");
+    }
+}

+ 20 - 0
授课代码/c260724/src/exercise/exercise03/Bird.java

@@ -0,0 +1,20 @@
+package exercise.exercise03;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Bird
+ * @description
+ * @create 2026/7/24
+ */
+public class Bird extends Animal{
+    public void eat(){
+        System.out.println("小鸟在啄米");
+    }
+    public void sleep(){
+        System.out.println("小鸟在巢穴里睡觉");
+    }
+    public void fly(){
+        System.out.println("小鸟在天空中飞翔~");
+    }
+}

+ 21 - 0
授课代码/c260724/src/exercise/exercise03/Fish.java

@@ -0,0 +1,21 @@
+package exercise.exercise03;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Fish
+ * @description
+ * @create 2026/7/24
+ */
+public class Fish extends Animal{
+
+    public void eat(){
+        System.out.println("鱼儿在吃饲料");
+    }
+    public void sleep(){
+        System.out.println("鱼儿睁着眼睛睡觉");
+    }
+    public void swim(){
+        System.out.println("鱼儿在水中游来游去~");
+    }
+}

+ 44 - 0
授课代码/c260724/src/exercise/exercise03/ZooKeeper.java

@@ -0,0 +1,44 @@
+package exercise.exercise03;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title ZooKeeper
+ * @description
+ * @create 2026/7/24
+ */
+public class ZooKeeper {
+
+    // 这个方法接收 Animal 类型的参数(多态)
+    // 要求:
+    // 1. 调用 eat() 和 sleep()
+    // 2. 使用 instanceof 判断是哪种动物
+    // 3. 如果是 Fish,向下转型并调用 swim()
+    // 4. 如果是 Bird,向下转型并调用 fly()
+    public void handleAnimal(Animal animal) {
+        // 请补充代码
+        if (animal instanceof Fish f){  //可以简写成这样。在判断是否属于Fish类型的时候顺便创建Fish类型的变量
+            //Fish f=(Fish) animal;
+            f.eat();
+            f.sleep();
+            f.swim();   //直接调用f的特有方法
+        }else if (animal instanceof Bird b){
+            b.eat();
+            b.sleep();
+            b.fly();
+        }
+    }
+
+    public static void main(String[] args) {
+        ZooKeeper keeper = new ZooKeeper();
+
+        // 使用向上转型创建对象
+        Animal a1 = new Fish();
+        Animal a2 = new Bird();
+
+        // 交给管理员处理
+        keeper.handleAnimal(a1);
+        keeper.handleAnimal(a2);
+    }
+
+}

+ 69 - 0
授课代码/c260724/src/exercise/exercise03/题目.md

@@ -0,0 +1,69 @@
+## 练习 3:向下转型 + instanceof——动物园管理员
+
+**难度**:⭐⭐⭐  
+**知识点**:向上转型、向下转型、instanceof 运算符、子类特有方法的调用
+
+**场景描述**:动物园里有多种动物,管理员需要根据不同的动物执行不同的操作。有些动物会游泳,有些动物会飞翔,这些是特有行为。
+
+**`Animal` 类**(父类):
+
+- **普通方法**:`void eat()` —— 输出 `"动物在吃东西"`
+- **普通方法**:`void sleep()` —— 输出 `"动物在睡觉"`
+
+**`Fish` 类**(继承 Animal):
+
+- **重写** `eat()` —— 输出 `"鱼儿在吃饲料"`
+- **重写** `sleep()` —— 输出 `"鱼儿睁着眼睛睡觉"`
+- **特有方法**:`void swim()` —— 输出 `"鱼儿在水中游来游去~"`
+
+**`Bird` 类**(继承 Animal):
+
+- **重写** `eat()` —— 输出 `"小鸟在啄米"`
+- **重写** `sleep()` —— 输出 `"小鸟在巢穴里睡觉"`
+- **特有方法**:`void fly()` —— 输出 `"小鸟在天空中飞翔~"`
+
+**请完成 `ZooKeeper` 类**:
+
+```java
+public class ZooKeeper {
+    // 这个方法接收 Animal 类型的参数(多态)
+    // 要求:
+    // 1. 调用 eat() 和 sleep()
+    // 2. 使用 instanceof 判断是哪种动物
+    // 3. 如果是 Fish,向下转型并调用 swim()
+    // 4. 如果是 Bird,向下转型并调用 fly()
+    public void handleAnimal(Animal animal) {
+        // 请补充代码
+        
+    }
+
+    public static void main(String[] args) {
+        ZooKeeper keeper = new ZooKeeper();
+        
+        // 使用向上转型创建对象
+        Animal a1 = new Fish();
+        Animal a2 = new Bird();
+        
+        // 交给管理员处理
+        keeper.handleAnimal(a1);
+        keeper.handleAnimal(a2);
+    }
+}
+```
+
+**预期输出**:
+
+```
+鱼儿在吃饲料
+鱼儿睁着眼睛睡觉
+鱼儿在水中游来游去~
+小鸟在啄米
+小鸟在巢穴里睡觉
+小鸟在天空中飞翔~
+```
+
+**思考题**:
+
+1. 如果调用 `keeper.handleAnimal(new Animal())` 会发生什么?`instanceof` 判断会进入哪个分支?
+2. 如果去掉 `instanceof` 判断,直接强制转型 `(Fish) animal`,当传入 `Bird` 对象时会怎样?
+3. 为什么需要向下转型?向上转型后直接调用 `animal.swim()` 可以吗?为什么?

+ 13 - 7
课堂练习题/20260724-多态练习.md

@@ -20,12 +20,14 @@
 创建一个宠物喂食体系,体验 **同一个方法调用,不同子类表现出不同的行为**。
 
 **`Pet` 类**(父类):
+
 - **普通方法**:`void eat()` —— 输出 `"宠物在吃饭..."`
 
 **`Dog` 类**(继承 Pet):
 - **重写** `eat()` —— 输出 `"小狗在啃骨头..."`
 
 **`Cat` 类**(继承 Pet):
+
 - **重写** `eat()` —— 输出 `"小猫在吃鱼..."`
 
 **测试类 `PetTest` 要求**:
@@ -69,12 +71,14 @@ public class PetTest {
 **`交通工具` 体系**:
 
 **`Vehicle` 类**(父类):
+
 - **成员变量**:`String name = "交通工具"`
 - **普通方法**:`void run()` —— 输出 `"交通工具在行驶..."`
 - **普通方法**:`void stop()` —— 输出 `"交通工具刹车停止"`
 - **静态方法**:`static void info()` —— 输出 `"Vehicle 静态方法"`
 
 **`Car` 类**(继承 Vehicle):
+
 - **成员变量**:`String name = "小汽车"`
 - **重写** `run()` —— 输出 `"小汽车在高速行驶..."`
 - **特有方法**:`void openSunroof()` —— 输出 `"打开天窗~"`
@@ -84,6 +88,7 @@ public class PetTest {
 编写 `VehicleTest` 类,创建一个 `Car` 对象,分别使用「父类引用指向子类对象」和「子类引用指向子类对象」两种方式,观察成员变量、成员方法、特有方法的访问差异。
 
 **模板代码**:
+
 ```java
 public class VehicleTest {
     public static void main(String[] args) {
@@ -280,13 +285,14 @@ feed(new Cat());
 
 1. **创建员工数组**:创建一个 `Employee[]` 数组,包含至少 5 名员工(各类型混合)
 
-   | 类型 | id | 姓名 | 参数 |
-   |------|----|------|------|
-   | FullTimeEmployee | 1001 | 张三 | 基本工资 8000,绩效 2000 |
-   | PartTimeEmployee | 2001 | 李四 | 时薪 50,工作 80 小时 |
-   | Intern | 3001 | 王五 | 津贴 2000 |
-   | FullTimeEmployee | 1002 | 赵六 | 基本工资 10000,绩效 3000 |
-   | PartTimeEmployee | 2002 | 孙七 | 时薪 45,工作 60 小时 |
+   ```java
+   // 示例数据
+   Employee emp1 = new FullTimeEmployee(1001, "张三", 8000, 2000);
+   Employee emp2 = new PartTimeEmployee(2001, "李四", 50, 80);
+   Employee emp3 = new Intern(3001, "王五", 2000);
+   Employee emp4 = new FullTimeEmployee(1002, "赵六", 10000, 3000);
+   Employee emp5 = new PartTimeEmployee(2002, "孙七", 45, 60);
+   ```
 
 2. **遍历数组输出基本信息**:调用每个元素的 `showInfo()` 方法