java之instanceof操作符

2020-08-26 15:11:00 浏览数 (3)

a intanceof A:判断a是否是类A的的一个实例,返回值为boolean

public class Person extends Object{}

public class Student extends Person{}

public class Graduate extends Person{}

代码语言:javascript复制
public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        Student s = new Student();
        Graduate g = new Graduate();    
        System.out.println(p instanceof Person);
        System.out.println(s instanceof Person);
        System.out.println(g instanceof Person);
    }
}

输出:

true

true

true

1 人点赞