instanceof 关键字

概述

instanceof 关键字用于检查对象是否为指定类型的实例,返回boolean值。

语法格式

object instanceof ClassName    // 检查是否为类的实例
object instanceof Interface    // 检查是否实现了接口

基本用法

class Animal {
    protected String name;

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

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }

    public void bark() {
        System.out.println(name + " 汪汪叫");
    }
}

interface Flyable {
    void fly();
}

class Bird extends Animal implements Flyable {
    public Bird(String name) {
        super(name);
    }

    @Override
    public void fly() {
        System.out.println(name + " 正在飞行");
    }
}

public class InstanceofExample {
    public static void main(String[] args) {
        Animal animal = new Animal("动物");
        Dog dog = new Dog("旺财");
        Bird bird = new Bird("小鸟");

        // 基本类型检查
        System.out.println("dog instanceof Dog: " + (dog instanceof Dog));         // true
        System.out.println("dog instanceof Animal: " + (dog instanceof Animal));   // true
        System.out.println("animal instanceof Dog: " + (animal instanceof Dog));   // false

        // 接口类型检查
        System.out.println("bird instanceof Flyable: " + (bird instanceof Flyable)); // true
        System.out.println("dog instanceof Flyable: " + (dog instanceof Flyable));   // false

        // null检查
        Animal nullAnimal = null;
        System.out.println("null instanceof Animal: " + (nullAnimal instanceof Animal)); // false
    }
}

多态中的类型检查

public class PolymorphismInstanceof {
    public static void handleAnimal(Animal animal) {
        if (animal instanceof Dog) {
            Dog dog = (Dog) animal;
            dog.bark();
        } else if (animal instanceof Bird) {
            Bird bird = (Bird) animal;
            bird.fly();
        } else {
            System.out.println("未知动物类型: " + animal.name);
        }
    }

    public static void checkFlyable(Object obj) {
        if (obj instanceof Flyable) {
            Flyable flyable = (Flyable) obj;
            flyable.fly();
        } else {
            System.out.println("对象不能飞行");
        }
    }

    public static void main(String[] args) {
        Animal[] animals = {
            new Dog("旺财"),
            new Bird("小鸟"),
            new Animal("普通动物")
        };

        for (Animal animal : animals) {
            handleAnimal(animal);
        }

        // 检查飞行能力
        checkFlyable(new Bird("老鹰"));
        checkFlyable(new Dog("哈士奇"));
    }
}

安全类型转换

public class SafeCasting {
    public static void processObject(Object obj) {
        // 安全的类型检查和转换
        if (obj instanceof String) {
            String str = (String) obj;
            System.out.println("字符串长度: " + str.length());
        } else if (obj instanceof Integer) {
            Integer num = (Integer) obj;
            System.out.println("数字平方: " + (num * num));
        } else if (obj instanceof Animal) {
            Animal animal = (Animal) obj;
            System.out.println("动物名称: " + animal.name);
        } else {
            System.out.println("未知类型: " + obj.getClass().getSimpleName());
        }
    }

    public static void main(String[] args) {
        Object[] objects = {
            "Hello World",
            42,
            new Dog("小狗"),
            3.14,
            new int[]{1, 2, 3}
        };

        for (Object obj : objects) {
            processObject(obj);
        }
    }
}

instanceof用于运行时类型检查,是实现类型安全的重要工具。

powered by Gitbook© 2025 编外计划 | 最后修改: 2025-07-28 16:25:54

results matching ""

    No results matching ""