不冷博客

多态和instanceof关键字(判断)-11月2日讲课内容

多态

二者具有直接或间接的继承关系时,父类引用指向子类对象,从而产生多种形态;接口的引用指向实现接口的类对象也是多态

特点

多态场景下,父类引用调用方法,如果被子类重写过,优先执行子类重写过后的方法

public class TestCar {
    public static void main(String[] args) {
        // 父类引用指向子类对象
        Vehicle vehicle = new Car();
        
        // 优先执行子类重写过的方法
        vehicle.run();    // Car run!!!
    }
}

class Vehicle {
    public void run() {
        System.out.println("Vehicle run!!!");
    }
}

class Car extends Vehicle {
    @Override
    public void run() {
        System.out.println("Car run!!!");
    }
}

应用场景一

使用父类作为方法形参实现多态,使方法参数的类型更为宽泛

public class TestCar {
    public static void main(String[] args) {        
        Vehicle vehicle = new Car();
        vehicle.type = "小汽车";
        
        Bike bike = new Bike();
        bike.type = "自行车";
        
        Bus bus = new Bus();
        bus.type = "公交车";
        
        Employee employee = new Employee("你的迪丽热巴");
        employee.goHome(vehicle);
        employee.goHome(bus);
    }
}

class Employee {
    String name;

    public Employee() {
    }
    
    public Employee(String name) {
        this.name = name;
    }

    public void goHome(Vehicle vehicle) {
        System.out.println(this.name + "乘坐" + vehicle.type + "交通工具回家");
    }
}

class Vehicle {
    String type;

    public void run() {
        System.out.println("Vehicle run!!!");
    }
}

class Bus extends Vehicle {
    @Override
    public void run() {
        System.out.println("Bus run!!!");
    }
}

class Car extends Vehicle {
    @Override
    public void run() {
        System.out.println("Car run!!!");
    }
}

class Bike extends Vehicle {
    @Override
    public void run() {
        System.out.println("Bike run!!!");
    }
}

结果

你的迪丽热巴乘坐小汽车回家
Car run!!!
你的迪丽热巴乘坐公交车回家
Bus run!!!

应用场景二

使用父类作为方法返回值实现多态,使方法可以返回不同子类对象

public Vehicle buyVehicle(int money) {
        Vehicle vehicle = null;

        if (money >= 100) {
            Bus bus = new Bus();
            bus.speed = 60;
            bus.price = 1230000.0;
            bus.seatNum = 16;
            bus.type = "公交车";
            vehicle = bus;

        } else if (money >= 30) {
            Car car = new Car();
            car.price = 310000.0;
            car.speed = 90;
            car.type = "小汽车";
            car.brand = "BMW";
            vehicle = car;

        } else if (money >= 1) {
            Bike bike = new Bike();
            bike.type = "捷安特自行车";
            bike.speed = 40;
            bike.price = 2000.0;
            bike.color = "红色";
            vehicle = bike;
        }

        return vehicle;
    }

向上装箱与向下拆箱

class Animal{}

class Cat extends Animal{}

class Dog extends Animal{}

class Fish extends Animal {}

public class Test {
    public static void main(String[] args) {
        showAnimal(new Animal());    // code.polymorphic.animal.Animal@7852e922
        // 向上转型
        showAnimal(new Cat());    // code.polymorphic.animal.Cat@4e25154f
        // 向上转型
        showAnimal(new Dog());    // code.polymorphic.animal.Dog@70dea4e
        // 向上转型
        showAnimal(new Fish());    // code.polymorphic.animal.Fish@5c647e05
        
        System.out.println("----------------------");
        
        Animal animal = getAnimal();
        // 向下转型
        Cat cat = (Cat) getCat();
        // 向下转型
        Dog dog = (Dog) getDog();
        // 向下转型
        Fish fish = (Fish) getFish();
        
        System.out.println(animal);    // code.polymorphic.animal.Animal@33909752
        System.out.println(cat);    // code.polymorphic.animal.Cat@55f96302
        System.out.println(dog);    // code.polymorphic.animal.Dog@3d4eac69
        System.out.println(fish);    // code.polymorphic.animal.Fish@42a57993
    }
    
    /**
     * 展示动物
     * @param animal
     */
    public static void showAnimal(Animal animal) {
        System.out.println(animal);
    }
    
    /**
     * 得到动物
     * @return 返回一个Animal对象
     */
    public static Animal getAnimal() {
        return new Animal();
    }
    
    /**
     * 得到猫
     * @return 返回一个Cat对象
     */
    public static Animal getCat() {
        return new Cat();
    }
    
    /**
     * 得到狗
     * @return 返回一个Dog对象
     */
    public static Animal getDog() {
        return new Dog();
    }
    
    /**
     * 得到鱼
     * @return 返回一个Fish对象
     */
    public static Animal getFish() {
        return new Fish();
    }
}

instanceof 关键字

用于判断当前对象是否是某个类,或者其子类、实现类的实例。如果是返回true,否则返回false。

/**
 * 动物类
 */
class Animal {
}

/**
 * 老虎类
 */
class Tiger extends Animal {
    
}

/**
 * 熊猫类
 */
class Panda extends Animal {
    
}

/**
 * 猴子类
 */
class Monkey extends Animal {
    
}

public class AnimalDemo {
    public static void main(String[] args) {
        Animal ani = getAnimal();
        
        if (ani instanceof Panda) {
            // ani一定是panda对象或子类对象
            Panda panda2 = (Panda) ani;
            System.out.println("这是熊猫:" + panda2);
            showPanda(panda2);
        } else {
            System.out.println("这是动物:" + ani);
        }
    }
    
    // 获取动物,返回一个Panda对象
    public static Animal getAnimal() {
        return new Panda();
    }
    
    // 展示熊猫对象
    public static void showPanda(Panda panda) {
        System.out.println(panda);
    }
}

【注意】使用 instanceof 关键字做判断时, instanceof 操作符的左操作数必须和右操作类或者接口存在继承或实现关系

总结

1、父类引用指向子类对象,接口引用指向实现类对象

2、instanceof用以比较对象是否是类或父类的实例,接口的实现类

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »