Java学习第3天 - 面向对象高级特性

学习时间: 4-5小时
学习目标: 掌握继承、多态、抽象类和接口等面向对象高级概念


📋 详细学习清单

✅ 第一部分:继承(Inheritance)深度理解 (70分钟)

1. 继承概念与JavaScript对比

JavaScript (你熟悉的原型继承)

javascript 复制代码
// ES6 类继承
class Animal {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    
    makeSound() {
        console.log(`${this.name} makes a sound`);
    }
    
    eat() {
        console.log(`${this.name} is eating`);
    }
}

class Dog extends Animal {
    constructor(name, age, breed) {
        super(name, age);
        this.breed = breed;
    }
    
    makeSound() {  // 重写父类方法
        console.log(`${this.name} barks: Woof!`);
    }
    
    wagTail() {    // 子类特有方法
        console.log(`${this.name} wags tail`);
    }
}

Java (今天学习的类继承)

java 复制代码
// Animal.java - 父类(基类/超类)
public class Animal {
    // 受保护的属性,子类可以访问
    protected String name;
    protected int age;
    
    // 构造函数
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Animal构造函数被调用");
    }
    
    // 普通方法
    public void makeSound() {
        System.out.println(name + " makes a sound");
    }
    
    public void eat() {
        System.out.println(name + " is eating");
    }
    
    // 获取动物信息
    public void showInfo() {
        System.out.println("动物信息: 姓名=" + name + ", 年龄=" + age);
    }
}

// Dog.java - 子类(派生类)
public class Dog extends Animal {
    private String breed;  // 子类特有属性
    
    // 子类构造函数
    public Dog(String name, int age, String breed) {
        super(name, age);  // 调用父类构造函数(必须是第一行)
        this.breed = breed;
        System.out.println("Dog构造函数被调用");
    }
    
    // 方法重写(Override)
    @Override
    public void makeSound() {
        System.out.println(name + " barks: Woof! Woof!");
    }
    
    // 子类特有方法
    public void wagTail() {
        System.out.println(name + " wags tail happily");
    }
    
    public void fetch() {
        System.out.println(name + " fetches the ball");
    }
    
    // 重写showInfo方法
    @Override
    public void showInfo() {
        super.showInfo();  // 调用父类方法
        System.out.println("品种: " + breed);
    }
}

2. 继承的核心概念详解

继承的关键字和语法:

java 复制代码
// 继承语法
class 子类名 extends 父类名 {
    // 子类内容
}

// super关键字的三种用法
public class Cat extends Animal {
    private String color;
    
    public Cat(String name, int age, String color) {
        super(name, age);        // 1. 调用父类构造函数
        this.color = color;
    }
    
    @Override
    public void makeSound() {
        super.makeSound();       // 2. 调用父类方法
        System.out.println("Meow! Meow!");
    }
    
    public void showParentName() {
        System.out.println("父类name: " + super.name); // 3. 访问父类属性
    }
}

💡 重要概念解释:

java 复制代码
/*
1. extends关键字:表示继承关系
2. super关键字:代表父类对象的引用
3. @Override注解:表示方法重写,编译器会检查
4. 构造函数调用顺序:父类构造函数 → 子类构造函数
5. Java单继承:一个类只能继承一个父类
*/

3. 实战练习:动物管理系统

java 复制代码
// AnimalTest.java - 测试类
public class AnimalTest {
    public static void main(String[] args) {
        System.out.println("=== 动物管理系统 ===\n");
        
        // 创建父类对象
        Animal animal = new Animal("通用动物", 5);
        animal.makeSound();
        animal.eat();
        animal.showInfo();
        
        System.out.println("\n" + "=".repeat(30) + "\n");
        
        // 创建子类对象
        Dog dog = new Dog("旺财", 3, "金毛");
        dog.makeSound();    // 调用重写的方法
        dog.eat();          // 继承的方法
        dog.wagTail();      // 子类特有方法
        dog.fetch();        // 子类特有方法
        dog.showInfo();     // 重写的方法
        
        System.out.println("\n" + "=".repeat(30) + "\n");
        
        Cat cat = new Cat("咪咪", 2, "白色");
        cat.makeSound();
        cat.showInfo();
    }
}

✅ 第二部分:多态(Polymorphism)深度理解 (70分钟)

1. 多态概念与实现

什么是多态?

java 复制代码
/*
多态 = 一个接口,多种实现
- 编译时多态:方法重载(Method Overloading)
- 运行时多态:方法重写(Method Overriding)+ 向上转型
*/

JavaScript中的多态(参考)

javascript 复制代码
// JavaScript中的多态更灵活(鸭子类型)
function makeAnimalSound(animal) {
    animal.makeSound();  // 不管是什么动物,只要有makeSound方法就行
}

let dog = { makeSound: () => console.log("Woof!") };
let cat = { makeSound: () => console.log("Meow!") };

makeAnimalSound(dog);  // Woof!
makeAnimalSound(cat);  // Meow!

Java中的多态(严格类型)

java 复制代码
// PolymorphismDemo.java
public class PolymorphismDemo {
    public static void main(String[] args) {
        System.out.println("=== Java多态演示 ===\n");
        
        // 1. 向上转型(Upcasting)- 自动转换
        Animal animal1 = new Dog("旺财", 3, "拉布拉多");
        Animal animal2 = new Cat("咪咪", 2, "橘色");
        Animal animal3 = new Bird("小鸟", 1, "蓝色");
        
        // 2. 多态调用 - 运行时决定调用哪个方法
        System.out.println("=== 多态方法调用 ===");
        animal1.makeSound();  // 调用Dog的makeSound
        animal2.makeSound();  // 调用Cat的makeSound
        animal3.makeSound();  // 调用Bird的makeSound
        
        // 3. 多态数组
        System.out.println("\n=== 多态数组演示 ===");
        Animal[] animals = {
            new Dog("大黄", 4, "中华田园犬"),
            new Cat("小白", 1, "波斯猫"),
            new Bird("啾啾", 2, "金丝雀"),
            new Dog("黑子", 5, "黑背")
        };
        
        // 统一处理不同类型的动物
        for (Animal animal : animals) {
            System.out.print("动物叫声: ");
            animal.makeSound();
            
            // 向下转型(Downcasting)- 需要强制转换
            if (animal instanceof Dog) {
                Dog dog = (Dog) animal;
                dog.wagTail();
            } else if (animal instanceof Cat) {
                Cat cat = (Cat) animal;
                cat.climb();
            } else if (animal instanceof Bird) {
                Bird bird = (Bird) animal;
                bird.fly();
            }
            System.out.println();
        }
    }
    
    // 多态方法 - 接受父类参数,可以传入任何子类对象
    public static void feedAnimal(Animal animal) {
        System.out.println("正在喂食...");
        animal.eat();
        animal.makeSound();
    }
}

// Bird.java - 新增鸟类
class Bird extends Animal {
    private String featherColor;
    
    public Bird(String name, int age, String featherColor) {
        super(name, age);
        this.featherColor = featherColor;
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " chirps: Tweet! Tweet!");
    }
    
    public void fly() {
        System.out.println(name + " is flying in the sky");
    }
    
    @Override
    public void showInfo() {
        super.showInfo();
        System.out.println("羽毛颜色: " + featherColor);
    }
}

2. instanceof 运算符详解

java 复制代码
// InstanceofDemo.java
public class InstanceofDemo {
    public static void main(String[] args) {
        Animal animal = new Dog("旺财", 3, "金毛");
        
        // instanceof 用于检查对象类型
        System.out.println("=== instanceof 运算符演示 ===");
        System.out.println("animal instanceof Animal: " + (animal instanceof Animal));  // true
        System.out.println("animal instanceof Dog: " + (animal instanceof Dog));        // true
        System.out.println("animal instanceof Cat: " + (animal instanceof Cat));        // false
        
        // 安全的向下转型
        if (animal instanceof Dog) {
            Dog dog = (Dog) animal;
            dog.wagTail();
            System.out.println("安全转型成功");
        } else {
            System.out.println("转型失败");
        }
        
        // 演示多态的好处
        processAnimal(new Dog("小黑", 2, "边牧"));
        processAnimal(new Cat("小花", 3, "三花"));
        processAnimal(new Bird("小红", 1, "红色"));
    }
    
    // 一个方法处理所有动物类型
    public static void processAnimal(Animal animal) {
        System.out.println("\n=== 处理动物: " + animal.name + " ===");
        animal.showInfo();
        animal.makeSound();
        animal.eat();
        
        // 根据具体类型执行特殊操作
        if (animal instanceof Dog) {
            ((Dog) animal).wagTail();
        } else if (animal instanceof Cat) {
            ((Cat) animal).climb();
        } else if (animal instanceof Bird) {
            ((Bird) animal).fly();
        }
    }
}

✅ 第三部分:抽象类(Abstract Class)详解 (60分钟)

1. 抽象类概念与语法

为什么需要抽象类?

java 复制代码
/*
问题:有些父类的方法无法给出具体实现
- 比如 Animal 类的 makeSound() 方法
- 不同动物的叫声完全不同
- 父类无法给出通用实现

解决方案:抽象类
- 包含抽象方法的类必须声明为抽象类
- 抽象类不能被实例化
- 子类必须实现所有抽象方法
*/

抽象类语法:

java 复制代码
// AbstractAnimal.java - 抽象动物类
public abstract class AbstractAnimal {
    // 普通属性
    protected String name;
    protected int age;
    protected String species;  // 物种
    
    // 普通构造函数
    public AbstractAnimal(String name, int age, String species) {
        this.name = name;
        this.age = age;
        this.species = species;
        System.out.println("抽象动物类构造函数被调用");
    }
    
    // 普通方法 - 有具体实现
    public void eat() {
        System.out.println(name + " is eating food");
    }
    
    public void sleep() {
        System.out.println(name + " is sleeping");
    }
    
    public void showBasicInfo() {
        System.out.println("动物基本信息:");
        System.out.println("姓名: " + name);
        System.out.println("年龄: " + age);
        System.out.println("物种: " + species);
    }
    
    // 抽象方法 - 没有具体实现,子类必须重写
    public abstract void makeSound();
    public abstract void move();
    public abstract void showSpecialSkill();
    
    // 抽象方法可以有参数和返回值
    public abstract String getHabitat();  // 获取栖息地
    public abstract int getLifespan();    // 获取寿命
}

2. 抽象类的实现

java 复制代码
// ConcreteDog.java - 具体的狗类
public class ConcreteDog extends AbstractAnimal {
    private String breed;
    private boolean isTrained;
    
    public ConcreteDog(String name, int age, String breed, boolean isTrained) {
        super(name, age, "犬科");  // 调用父类构造函数
        this.breed = breed;
        this.isTrained = isTrained;
    }
    
    // 必须实现所有抽象方法
    @Override
    public void makeSound() {
        System.out.println(name + " barks loudly: Woof! Woof!");
        if (isTrained) {
            System.out.println(name + " 是训练有素的狗狗");
        }
    }
    
    @Override
    public void move() {
        System.out.println(name + " runs on four legs very fast");
    }
    
    @Override
    public void showSpecialSkill() {
        if (isTrained) {
            System.out.println(name + " 会握手、坐下、装死等技能");
        } else {
            System.out.println(name + " 还没有接受训练");
        }
    }
    
    @Override
    public String getHabitat() {
        return "人类家庭或犬舍";
    }
    
    @Override
    public int getLifespan() {
        return 12;  // 狗的平均寿命12年
    }
    
    // 子类特有方法
    public void guard() {
        System.out.println(name + " is guarding the house");
    }
    
    public void showFullInfo() {
        showBasicInfo();
        System.out.println("品种: " + breed);
        System.out.println("是否训练: " + (isTrained ? "是" : "否"));
        System.out.println("栖息地: " + getHabitat());
        System.out.println("预期寿命: " + getLifespan() + "年");
    }
}

// ConcreteCat.java - 具体的猫类
public class ConcreteCat extends AbstractAnimal {
    private String furColor;
    private boolean isIndoor;
    
    public ConcreteCat(String name, int age, String furColor, boolean isIndoor) {
        super(name, age, "猫科");
        this.furColor = furColor;
        this.isIndoor = isIndoor;
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " meows softly: Meow~ Meow~");
        if (isIndoor) {
            System.out.println(name + " 是室内猫,叫声比较温柔");
        }
    }
    
    @Override
    public void move() {
        System.out.println(name + " walks gracefully and can jump very high");
    }
    
    @Override
    public void showSpecialSkill() {
        System.out.println(name + " 擅长爬树、夜视、抓老鼠");
        if (isIndoor) {
            System.out.println(name + " 还会使用猫砂盆");
        }
    }
    
    @Override
    public String getHabitat() {
        return isIndoor ? "室内环境" : "室内外自由活动";
    }
    
    @Override
    public int getLifespan() {
        return isIndoor ? 16 : 13;  // 室内猫寿命更长
    }
    
    public void purr() {
        System.out.println(name + " purrs contentedly: Purrrr~");
    }
    
    public void showFullInfo() {
        showBasicInfo();
        System.out.println("毛色: " + furColor);
        System.out.println("生活环境: " + (isIndoor ? "室内" : "室内外"));
        System.out.println("栖息地: " + getHabitat());
        System.out.println("预期寿命: " + getLifespan() + "年");
    }
}

3. 抽象类测试与练习

java 复制代码
// AbstractClassTest.java
public class AbstractClassTest {
    public static void main(String[] args) {
        System.out.println("=== 抽象类演示程序 ===\n");
        
        // 不能实例化抽象类
        // AbstractAnimal animal = new AbstractAnimal(); // 编译错误
        
        // 创建具体子类对象
        ConcreteDog dog = new ConcreteDog("大黄", 3, "金毛寻回犬", true);
        ConcreteCat cat = new ConcreteCat("小花", 2, "三花色", true);
        
        System.out.println("=== 狗狗信息 ===");
        dog.showFullInfo();
        System.out.println("\n=== 狗狗行为 ===");
        dog.makeSound();
        dog.move();
        dog.eat();          // 继承的普通方法
        dog.showSpecialSkill();
        dog.guard();        // 子类特有方法
        
        System.out.println("\n" + "=".repeat(50) + "\n");
        
        System.out.println("=== 猫咪信息 ===");
        cat.showFullInfo();
        System.out.println("\n=== 猫咪行为 ===");
        cat.makeSound();
        cat.move();
        cat.sleep();        // 继承的普通方法
        cat.showSpecialSkill();
        cat.purr();         // 子类特有方法
        
        System.out.println("\n" + "=".repeat(50) + "\n");
        
        // 多态使用抽象类
        System.out.println("=== 抽象类多态演示 ===");
        AbstractAnimal[] animals = {
            new ConcreteDog("小黑", 4, "边境牧羊犬", false),
            new ConcreteCat("小白", 1, "纯白色", false),
            new ConcreteDog("旺财", 5, "中华田园犬", true)
        };
        
        for (AbstractAnimal animal : animals) {
            System.out.println("\n--- 处理动物: " + animal.name + " ---");
            animal.showBasicInfo();
            animal.makeSound();
            animal.move();
            System.out.println("栖息地: " + animal.getHabitat());
            System.out.println("预期寿命: " + animal.getLifespan() + "年");
            
            // 类型判断和特殊处理
            if (animal instanceof ConcreteDog) {
                ((ConcreteDog) animal).guard();
            } else if (animal instanceof ConcreteCat) {
                ((ConcreteCat) animal).purr();
            }
        }
    }
}

✅ 第四部分:接口(Interface)详解 (60分钟)

1. 接口概念与JavaScript对比

JavaScript中的"接口"概念(非正式)

javascript 复制代码
// JavaScript没有真正的接口,但可以用约定来模拟
const Flyable = {
    fly: function() {
        throw new Error("fly method must be implemented");
    }
};

const Swimmable = {
    swim: function() {
        throw new Error("swim method must be implemented");
    }
};

// "实现"多个接口
class Duck {
    fly() {
        console.log("Duck is flying");
    }
    
    swim() {
        console.log("Duck is swimming");
    }
}

Java中的正式接口

java 复制代码
// Flyable.java - 飞行接口
public interface Flyable {
    // 接口中的变量自动是 public static final
    int MAX_ALTITUDE = 10000;  // 最大飞行高度
    
    // 接口中的方法自动是 public abstract
    void fly();
    void land();
    
    // Java 8+ 可以有默认方法
    default void showFlyingInfo() {
        System.out.println("这是一个会飞行的生物,最大飞行高度: " + MAX_ALTITUDE + "米");
    }
    
    // Java 8+ 可以有静态方法
    static void compareAltitude(int altitude1, int altitude2) {
        System.out.println("高度比较: " + 
            (altitude1 > altitude2 ? "第一个更高" : "第二个更高"));
    }
}

// Swimmable.java - 游泳接口
public interface Swimmable {
    double MAX_DEPTH = 1000.0;  // 最大游泳深度
    
    void swim();
    void dive();
    
    default void showSwimmingInfo() {
        System.out.println("这是一个会游泳的生物,最大游泳深度: " + MAX_DEPTH + "米");
    }
}

// Runnable.java - 奔跑接口(注意:不要与java.lang.Runnable冲突)
public interface AnimalRunnable {
    int MAX_SPEED = 100;  // 最大奔跑速度 km/h
    
    void run();
    void rest();
    
    default void showRunningInfo() {
        System.out.println("这是一个会奔跑的生物,最大速度: " + MAX_SPEED + "km/h");
    }
}

2. 接口的实现

java 复制代码
// Duck.java - 鸭子类实现多个接口
public class Duck extends AbstractAnimal implements Flyable, Swimmable, AnimalRunnable {
    private String duckType;
    private boolean canFlyWell;
    
    public Duck(String name, int age, String duckType, boolean canFlyWell) {
        super(name, age, "鸭科");
        this.duckType = duckType;
        this.canFlyWell = canFlyWell;
    }
    
    // 实现抽象类的抽象方法
    @Override
    public void makeSound() {
        System.out.println(name + " quacks: Quack! Quack!");
    }
    
    @Override
    public void move() {
        System.out.println(name + " can walk, swim, and " + 
            (canFlyWell ? "fly well" : "fly short distances"));
    }
    
    @Override
    public void showSpecialSkill() {
        System.out.println(name + " 擅长游泳、潜水,还能飞行");
        if (duckType.equals("野鸭")) {
            System.out.println(name + " 是候鸟,会长距离飞行");
        }
    }
    
    @Override
    public String getHabitat() {
        return "湖泊、河流、池塘等水域环境";
    }
    
    @Override
    public int getLifespan() {
        return 8;
    }
    
    // 实现Flyable接口
    @Override
    public void fly() {
        if (canFlyWell) {
            System.out.println(name + " 飞得很高很远,最高可达 " + MAX_ALTITUDE/10 + " 米");
        } else {
            System.out.println(name + " 只能短距离飞行");
        }
    }
    
    @Override
    public void land() {
        System.out.println(name + " 缓缓降落在水面上");
    }
    
    // 实现Swimmable接口
    @Override
    public void swim() {
        System.out.println(name + " 在水面上优雅地游泳");
    }
    
    @Override
    public void dive() {
        System.out.println(name + " 潜入水中寻找食物,最深可达 " + MAX_DEPTH/100 + " 米");
    }
    
    // 实现AnimalRunnable接口
    @Override
    public void run() {
        System.out.println(name + " 在陆地上摇摆着快速奔跑");
    }
    
    @Override
    public void rest() {
        System.out.println(name + " 在水边休息,头塞在翅膀下");
    }
    
    // 子类特有方法
    public void showAllAbilities() {
        System.out.println(name + " 的全部能力展示:");
        showFlyingInfo();    // 接口默认方法
        showSwimmingInfo();  // 接口默认方法
        showRunningInfo();   // 接口默认方法
    }
}

// Eagle.java - 老鹰类(只会飞和跑)
public class Eagle extends AbstractAnimal implements Flyable, AnimalRunnable {
    private double wingspan;  // 翼展
    
    public Eagle(String name, int age, double wingspan) {
        super(name, age, "鹰科");
        this.wingspan = wingspan;
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " screeches: Screech! Screech!");
    }
    
    @Override
    public void move() {
        System.out.println(name + " 主要通过飞行移动,翼展达 " + wingspan + " 米");
    }
    
    @Override
    public void showSpecialSkill() {
        System.out.println(name + " 拥有超强的视力和飞行能力,是天空中的霸主");
    }
    
    @Override
    public String getHabitat() {
        return "山区、悬崖、高大树木";
    }
    
    @Override
    public int getLifespan() {
        return 20;
    }
    
    @Override
    public void fly() {
        System.out.println(name + " 翱翔在高空,可达 " + MAX_ALTITUDE + " 米高度");
    }
    
    @Override
    public void land() {
        System.out.println(name + " 精准地降落在高处的巢穴中");
    }
    
    @Override
    public void run() {
        System.out.println(name + " 在地面上快速奔跑追击猎物");
    }
    
    @Override
    public void rest() {
        System.out.println(name + " 栖息在高处,警惕地观察四周");
    }
    
    public void hunt() {
        System.out.println(name + " 从高空俯冲捕猎");
    }
}

3. 接口的高级特性测试

java 复制代码
// InterfaceTest.java
public class InterfaceTest {
    public static void main(String[] args) {
        System.out.println("=== 接口演示程序 ===\n");
        
        // 创建实现了多个接口的对象
        Duck duck = new Duck("唐老鸭", 3, "家鸭", false);
        Eagle eagle = new Eagle("金雕", 5, 2.3);
        
        System.out.println("=== 鸭子的多种能力 ===");
        duck.showAllAbilities();
        
        System.out.println("\n=== 鸭子行为演示 ===");
        // 飞行能力
        duck.fly();
        duck.land();
        
        // 游泳能力
        duck.swim();
        duck.dive();
        
        // 奔跑能力
        duck.run();
        duck.rest();
        
        // 基本动物行为
        duck.makeSound();
        duck.eat();
        
        System.out.println("\n" + "=".repeat(50) + "\n");
        
        System.out.println("=== 老鹰行为演示 ===");
        eagle.showFlyingInfo();
        eagle.showRunningInfo();
        eagle.fly();
        eagle.run();
        eagle.hunt();
        eagle.makeSound();
        
        System.out.println("\n" + "=".repeat(50) + "\n");
        
        // 接口多态演示
        System.out.println("=== 接口多态演示 ===");
        
        // 飞行接口多态
        Flyable[] flyingAnimals = {duck, eagle};
        System.out.println("所有会飞的动物:");
        for (Flyable flyer : flyingAnimals) {
            flyer.fly();
            flyer.land();
            flyer.showFlyingInfo();
            System.out.println();
        }
        
        // 奔跑接口多态
        AnimalRunnable[] runningAnimals = {duck, eagle};
        System.out.println("所有会奔跑的动物:");
        for (AnimalRunnable runner : runningAnimals) {
            runner.run();
            runner.rest();
            System.out.println();
        }
        
        // 接口静态方法调用
        System.out.println("=== 接口静态方法演示 ===");
        Flyable.compareAltitude(5000, 8000);
        
        // 测试接口常量
        System.out.println("飞行最大高度: " + Flyable.MAX_ALTITUDE + "米");
        System.out.println("游泳最大深度: " + Swimmable.MAX_DEPTH + "米");
        System.out.println("奔跑最大速度: " + AnimalRunnable.MAX_SPEED + "km/h");
    }
}

✅ 第五部分:综合实战项目 (45分钟)

动物园管理系统

java 复制代码
// Zoo.java - 动物园管理系统
import java.util.ArrayList;
import java.util.List;

public class Zoo {
    private String zooName;
    private List<AbstractAnimal> animals;
    private List<Flyable> flyingShow;      // 飞行表演团
    private List<Swimmable> swimmingShow;  // 游泳表演团
    
    public Zoo(String zooName) {
        this.zooName = zooName;
        this.animals = new ArrayList<>();
        this.flyingShow = new ArrayList<>();
        this.swimmingShow = new ArrayList<>();
    }
    
    // 添加动物到动物园
    public void addAnimal(AbstractAnimal animal) {
        animals.add(animal);
        System.out.println(animal.name + " 已加入 " + zooName);
        
        // 如果会飞,加入飞行表演团
        if (animal instanceof Flyable) {
            flyingShow.add((Flyable) animal);
            System.out.println(animal.name + " 已加入飞行表演团");
        }
        
        // 如果会游泳,加入游泳表演团
        if (animal instanceof Swimmable) {
            swimmingShow.add((Swimmable) animal);
            System.out.println(animal.name + " 已加入游泳表演团");
        }
    }
    
    // 展示所有动物
    public void showAllAnimals() {
        System.out.println("\n=== " + zooName + " 动物名单 ===");
        for (int i = 0; i < animals.size(); i++) {
            AbstractAnimal animal = animals.get(i);
            System.out.println((i + 1) + ". " + animal.name + " (" + animal.species + ")");
        }
    }
    
    // 喂食时间
    public void feedingTime() {
        System.out.println("\n=== " + zooName + " 喂食时间 ===");
        for (AbstractAnimal animal : animals) {
            System.out.println("正在喂食 " + animal.name + ":");
            animal.eat();
            animal.makeSound();  // 动物表达满足
            System.out.println();
        }
    }
    
    // 飞行表演
    public void flyingPerformance() {
        if (flyingShow.isEmpty()) {
            System.out.println("今天没有飞行表演");
            return;
        }
        
        System.out.println("\n=== " + zooName + " 飞行表演开始 ===");
        for (Flyable performer : flyingShow) {
            if (performer instanceof AbstractAnimal) {
                AbstractAnimal animal = (AbstractAnimal) performer;
                System.out.println(animal.name + " 的飞行表演:");
                performer.fly();
                performer.land();
                animal.makeSound();
                System.out.println();
            }
        }
        System.out.println("飞行表演结束,感谢观看!");
    }
    
    // 游泳表演
    public void swimmingPerformance() {
        if (swimmingShow.isEmpty()) {
            System.out.println("今天没有游泳表演");
            return;
        }
        
        System.out.println("\n=== " + zooName + " 游泳表演开始 ===");
        for (Swimmable performer : swimmingShow) {
            if (performer instanceof AbstractAnimal) {
                AbstractAnimal animal = (AbstractAnimal) performer;
                System.out.println(animal.name + " 的游泳表演:");
                performer.swim();
                performer.dive();
                animal.makeSound();
                System.out.println();
            }
        }
        System.out.println("游泳表演结束,感谢观看!");
    }
    
    // 动物健康检查
    public void healthCheck() {
        System.out.println("\n=== " + zooName + " 动物健康检查 ===");
        for (AbstractAnimal animal : animals) {
            System.out.println("检查 " + animal.name + ":");
            animal.showBasicInfo();
            System.out.println("栖息地需求: " + animal.getHabitat());
            System.out.println("预期寿命: " + animal.getLifespan() + "年");
            System.out.println("健康状态: 良好");
            System.out.println();
        }
    }
}

// ZooManagement.java - 动物园管理主程序
public class ZooManagement {
    public static void main(String[] args) {
        System.out.println("🎪 欢迎来到动物园管理系统 🎪\n");
        
        // 创建动物园
        Zoo wonderlandZoo = new Zoo("奇幻动物园");
        
        // 创建各种动物
        Duck mallard = new Duck("绿头鸭", 2, "野鸭", true);
        Duck domesticDuck = new Duck("小黄鸭", 1, "家鸭", false);
        Eagle goldenEagle = new Eagle("金雕", 4, 2.2);
        Eagle baldEagle = new Eagle("白头海雕", 6, 2.5);
        ConcreteDog husky = new ConcreteDog("哈士奇", 3, "西伯利亚雪橇犬", true);
        ConcreteCat persianCat = new ConcreteCat("波斯猫", 2, "银渐层", true);
        
        // 添加动物到动物园
        System.out.println("=== 动物入园 ===");
        wonderlandZoo.addAnimal(mallard);
        wonderlandZoo.addAnimal(domesticDuck);
        wonderlandZoo.addAnimal(goldenEagle);
        wonderlandZoo.addAnimal(baldEagle);
        wonderlandZoo.addAnimal(husky);
        wonderlandZoo.addAnimal(persianCat);
        
        // 展示动物园的一天
        wonderlandZoo.showAllAnimals();
        wonderlandZoo.healthCheck();
        wonderlandZoo.feedingTime();
        wonderlandZoo.flyingPerformance();
        wonderlandZoo.swimmingPerformance();
        
        System.out.println("\n🎉 今天的动物园活动圆满结束!🎉");
    }
}

📝 今日学习总结

✅ 已掌握的核心概念

  1. 继承(Inheritance)

    • extends关键字使用
    • super关键字三种用法
    • 方法重写@Override
    • 构造函数调用顺序
  2. 多态(Polymorphism)

    • 向上转型和向下转型
    • instanceof运算符
    • 多态数组和方法参数
    • 运行时方法绑定
  3. 抽象类(Abstract Class)

    • abstract关键字
    • 抽象方法和具体方法结合
    • 强制子类实现抽象方法
    • 不能实例化抽象类
  4. 接口(Interface)

    • 多接口实现
    • 接口常量和默认方法
    • 接口多态
    • 解决多重继承问题

🎯 明日学习预告

第4天:集合框架与泛型

  • ArrayList, HashMap基础使用
  • 泛型概念和语法
  • 迭代器和增强for循环
  • 集合的常用操作

🏠 今日作业

  1. 创建一个交通工具继承体系(车、船、飞机)
  2. 为交通工具添加相关接口(载客、载货、维修)
  3. 实现一个简单的交通工具管理系统
相关推荐
黎䪽圓19 分钟前
【Java多线程从青铜到王者】阻塞队列(十)
java·开发语言
Roam-G33 分钟前
List ToMap优化优化再优化到极致
java·list
"匠"人39 分钟前
讲解负载均衡
java·运维·负载均衡
C雨后彩虹1 小时前
行为模式-责任链模式
java·设计模式·责任链模式
写bug写bug1 小时前
搞懂Spring Cloud Config配置信息自动更新原理
java·后端·架构
异常君1 小时前
Dubbo 高可用性核心机制详解与实战(上)
java·dubbo·设计
trow1 小时前
Web 认证技术的演进:解决无状态与有状态需求的矛盾
java
快乐肚皮1 小时前
快速排序优化技巧详解:提升性能的关键策略
java·算法·性能优化·排序算法
网安INF1 小时前
SHA-1算法详解:原理、特点与应用
java·算法·密码学