详细理解,Java中多态特性的instanceof的使用!

点击查看代码
package com.oop.demo07;

public class Person {
    public void run(){
        System.out.println("Person-->run()");
    }
}
点击查看代码
package com.oop.demo07;

public class Teacher extends Person{
}
点击查看代码
package com.oop.demo07;

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("Student-->run()");
    }
    public void eat() {
        System.out.println("Student-->eat()");
    }
}
点击查看代码
package com.oop.demo07;

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object o = new Student();
        System.out.println(o instanceof Student);//true
        System.out.println(o instanceof Person);//true
        System.out.println(o instanceof Teacher);//false
        System.out.println(o instanceof String);//false
        System.out.println("================");
        Person person1 = new Student();
        System.out.println(person1 instanceof Person);//true
        System.out.println(person1 instanceof Student);//true
        System.out.println(person1 instanceof Teacher);//false
        System.out.println(person1 instanceof Object);//true
        //System.out.println(person1 instanceof String);编译报错
        System.out.println("================");
        Student student1 = new Student();
        System.out.println(student1 instanceof Person);//true
        System.out.println(student1 instanceof Student);//true
        //System.out.println(student1 instanceof Teacher);编译报错
        System.out.println(student1 instanceof Object);//true
        //System.out.println(student1 instanceof String);编译报错

    }
}
  1. 首先,要先搞懂一个前提:多态中的表面类型实际类型
    在Java中存在多态特性,简单说就是:父类类型的变量,可以指向子类的对象
    比如:
    Person person = new Student(); // Person是父类,Student是子类

    • 表面类型(声明类型):Person(编译时的类型)
    • 实际类型(运行时类型):Student(真正new出来的对象类型)
  2. instanceof的作用: 判断对象的实际类型
    instanceof的本质:透过表面类型,判断对象的实际类型
    instanceof的核心逻辑:判断“new出来的实际对象”是否是“右侧类型”的实例(包括直接 / 间接继承、实现关系)
    语法:对象 instanceof 类型 ---> 返回true/false(是不是这个类型的实例)

    例子:

    Object o = new Student();
    
    /*
    实际对象是 Student(new Student());
    右侧类型是 Student;
    结论:Student 对象当然是 Student 类的实例 → true。
    */
    System.out.println(o instanceof Student);//true
    /*
    实际对象是 Student;
    右侧类型是 Person;
    因为 Student 继承自 Person(子类是父类的 “实例”),就像 “学生是人的一种”;
    结论:Student 对象也是 Person 类的实例 → true。
    */
    System.out.println(o instanceof Person);//true
    /*
    实际对象是 Student;
    右侧类型是 Teacher;
    Student 和 Teacher 是 平级关系(都是 Person 的子类,但互不继承);
    结论:Student 对象不是 Teacher 类的实例 → false。
    */
    System.out.println(o instanceof Teacher);//false
    /*
    实际对象是 Student;
    右侧类型是 String;
    Student 和 String 没有任何继承关系(两者唯一的共同父类是 Object,但彼此无关);
    结论:Student 对象不是 String 类的实例 → false。
    */
    System.out.println(o instanceof String);//false
    

    即使表面类型是Object,instanceof也能识别出它的实际类型是Student
    instanceof的判断只关心“实际创建的对象类型”和“右侧类型的继承关系”,与左侧引用变量的声明类型(如这里的Object)无关。

  3. instanceof的核心用途:安全地调用子类特有方法
    父类Person可能只有通用方法(如run()),而子类Student有自己的特有方法(如eat())。如果想要调用子类特有的方法,必须先把父类类型的变量强制转换为子类类型。

    但是强制转换有风险:如果实际类型不匹配,会报错ClassCastException。

    所以,instanceof的作用就是在转换前先检查,确保安全。

posted @ 2025-08-14 20:21  wjfreedomking  阅读(19)  评论(0)    收藏  举报