智能医学工程-计算机程序设计-2025版-教学案例-第10章

7 阅读4分钟

eg 10.1

class Animal {
    public String name;
    public int age;
    public String sex;
    
    public void eat() {
        System.out.println(this.name + "eat()!");
    }
    
    public void sleep() {
        System.out.println(this.name + "sleep()!");
    }
}

class Cat extends Animal {
    public void mew() {
        System.out.println(this.name + "cat::mew()!");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println(this.name + "dog::bark()!");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.name = "咪咪";
        cat.age = 6;
        cat.sex = "女";
        cat.sleep();
        cat.mew();
        cat.eat();
    }
}

eg 10.1-2

class Base {
    public int a = 10;
    public int b = 20;
    public int c = 30;
}

class Derived extends Base {
    public int a = 100; // 与父类中成员a同名,且类型相同
    public char b = 'x'; // 与父类中成员b同名,但类型不同
    
    public void method() {
        System.out.println(a);       // 100, 子类和父类都有成员变量a,优先访问子类的a
        System.out.println(b);       // x, 子类和父类都有成员变量b,但类型不同,访问子类的b
        System.out.println(c);       // 30, 子类没有c,访问父类的c
        System.out.println(super.a); // 10, 访问父类的a
        System.out.println("=======================");
        System.out.println(super.c); // 30
        System.out.println(this.c);  // 30
    }
}

public class TestDemo2 {
    public static void main(String[] args) {
        Derived derived = new Derived();
        derived.method();
    }
}

eg 10.1-3

public class Base {
    public void methodA() {
        System.out.println("Base中的methodA()");
    }
}

public class Derived extends Base {
    public void methodB() {
        System.out.println("Derived中的methodB()方法");
    }
    
    public void methodC() {
        methodB(); // 访问子类自己的methodB()
        methodA(); // 访问父类继承的methodA()
        // methodD(); // 编译失败,无methodD()
    }
}

eg 10.1-4

public class Base {
    public void methodA() {
        System.out.println("Base中的methodA()");
    }
    
    public void methodB() {
        System.out.println("Base中的methodB()");
    }
}

public class Derived extends Base {
    public void methodA(int a) {
        System.out.println("Derived中的method(int)方法");
    }
    
    public void methodB() {
        System.out.println("Derived中的methodB()方法");
    }
    
    public void methodC() {
        methodA();    // 访问父类methodA()
        methodA(20);  // 访问子类methodA(int)
        methodB();    // 访问子类methodB()
    }
}

eg 10.1-5

class Base {
    int a;
    int b;
    
    public void methodA() {
        System.out.println("Base中的methodA()");
    }
    
    public void methodB() {
        System.out.println("Base中的methodB()");
    }
}

class Derived extends Base {
    int a; // 与父类同名且类型相同
    char b; // 与父类同名但类型不同
    
    public void methodA(int a) {
        System.out.println("Derived中的method()方法");
    }
    
    public void methodB() {
        System.out.println("Derived中的methodB()方法");
    }
    
    public void methodC() {
        a = 100;        // 子类a
        b = 101;        // 子类b
        super.a = 200;  // 父类a
        super.b = 201;  // 父类b
        
        methodA();      // 父类methodA()
        methodA(20);    // 子类methodA(int)
        methodB();      // 子类methodB()
        super.methodB();// 父类methodB()
    }
}

eg 10.1-6

class Base {
    public Base() {
        System.out.println("Base()");
    }
}

class Derived extends Base {
    public Derived() {
        // super() 默认存在,必须为第一行
        System.out.println("Derived()");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Derived d = new Derived();
    }
}

eg 10.1-7

class Person {
    public String name;
    public int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("构造方法执行");
    }
    
    {
        System.out.println("实例代码块执行");
    }
    
    static {
        System.out.println("静态代码块执行");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Person person1 = new Person("frost",10);
        System.out.println("============================");
        Person person2 = new Person("cold",20);
    }
}

eg 10.1-8

class Animal {
    public String name;
    public int age;
    
    static {
        System.out.println("Animal的静态代码块。");
    }
    
    {
        System.out.println("Animal的实例代码块。");
    }
    
    public Animal() {
        System.out.println("Animal的不带参数的构造方法。");
    }
    
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Animal的带两个参数的构造方法。");
    }
    
    public void eat() {
        System.out.println(this.name + "eat()!");
    }
    
    public void sleep() {
        System.out.println(this.name + "sleep()!");
    }
}

class Cat extends Animal {
    public String fur;
    
    static {
        System.out.println("Cat的静态代码块。");
    }
    
    {
        System.out.println("Cat的实例代码块。");
    }
    
    public Cat() {
        super(); // 必须放在第一行
        System.out.println("Cat的不带参数的构造方法。");
    }
    
    public Cat(String name, int age, String fur) {
        super(name, age); // 显示调用父类构造方法
        this.fur = fur;
        System.out.println("Cat的带三个参数的构造方法。");
    }
    
    public void mew() {
        System.out.println(this.name + "cat::mew()!");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Cat cat1 = new Cat("咪咪", 2, "black");
        System.out.println("==============================");
        Cat cat2 = new Cat("喵喵", 3, "orange");
    }
}

eg 10.1-9

// 轮胎类
class Tire {
    // ...
}

// 发动机类
class Engine {
    // ...
}

// 车载系统类
class VehicleSystem {
    // ...
}

class Car {
    private Tire tire;           // 组合轮胎
    private Engine engine;       // 组合发动机
    private VehicleSystem vs;    // 组合车载系统
    // ...
}

// 奔驰是汽车
class Benz extends Car {
    // 继承汽车的所有组合属性
}

eg 10.1-10

class Shapes {
    public void draw() {
        System.out.println("画图");
    }
}

class Cycle extends Shapes {
    @Override
    public void draw() {
        System.out.println("●");
    }
}

class Rect extends Shapes {
    @Override
    public void draw() {
        System.out.println("◇");
    }
}

class Flower extends Shapes {
    @Override
    public void draw() {
        System.out.println("❀");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Shapes[] shapes = {new Cycle(), new Rect(), new Flower()};
        for (Shapes shape : shapes) {
            shape.draw();
        }
    }
}

eg 10.1-11

class Animal {
    String name;
    int age;
    
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void eat() {
        System.out.println(name + "吃饭");
    }
}

class Cat extends Animal {
    public Cat(String name, int age) {
        super(name, age);
    }
    
    @Override
    public void eat() {
        System.out.println(name + "吃鱼~~~");
    }
}

class Dog extends Animal {
    public Dog(String name, int age) {
        super(name, age);
    }
    
    @Override
    public void eat() {
        System.out.println(name + "吃骨头~~~");
    }
}

public class TestDemo {
    public static void eat(Animal a) {
        a.eat(); // 动态绑定
    }
    
    public static void main(String[] args) {
        eat(new Cat("咪咪", 2));
        eat(new Dog("旺财", 1));
    }
}

eg 10.1-12

class Animal {
    String name;
    int age;
    
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void eat() {
        System.out.println(name + "吃饭");
    }
}

class Cat extends Animal {
    public Cat(String name, int age) {
        super(name, age);
    }
    
    @Override
    public void eat() {
        System.out.println(name + "吃鱼~~~");
    }
    
    public void run() {
        System.out.println(name + "跑");
    }
}

class Dog extends Animal {
    public Dog(String name, int age) {
        super(name, age);
    }
    
    @Override
    public void eat() {
        System.out.println(name + "吃骨头~~~");
    }
    
    public void swim() {
        System.out.println(name + "游泳");
    }
}

public class TestAnimal {
    public static void main(String[] args) {
        Cat cat = new Cat("猫猫", 10);
        Dog dog = new Dog("旺旺", 6);
        
        Animal animal = cat;
        animal.eat(); // 调用Cat的eat()
        
        animal = dog;
        animal.eat(); // 调用Dog的eat()
        
        if (animal instanceof Cat) {
            ((Cat) animal).run();
        }
        if (animal instanceof Dog) {
            ((Dog) animal).swim();
        }
    }
}

eg 10.1-13

class B {
    public B() {
        func(); // 调用被子类重写的方法
    }
    
    public void func() {
        System.out.println("B.func()");
    }
}

class D extends B {
    private int num = 1;
    
    @Override
    public void func() {
        System.out.println("D.func() " + num); // num此时未初始化
    }
}

public class Test {
    public static void main(String[] args) {
        D d = new D(); // 输出:D.func() 0
    }
}

eg 10.1-14

class Parent {
    void parentMethod() {
        System.out.println("Parent method");
    }
}

class Child extends Parent {
    void childMethod() {
        System.out.println("Child method");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent parent = new Child(); // 向上转型
        
        parent.parentMethod(); // 调用父类方法
        
        if (parent instanceof Child) {
            Child child = (Child) parent; // 向下转型
            child.childMethod(); // 调用子类方法
        }
        
        // 错误示例:
        // Parent wrongParent = new Parent();
        // Child wrongChild = (Child) wrongParent; // ClassCastException
    }
}