Fork me on GitHub

java之instanceof操作符

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

public class Person extends Object{}

public class Student extends Person{}

public class Graduate extends Person{}

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

posted @ 2019-12-16 14:51  西西嘛呦  阅读(282)  评论(0编辑  收藏  举报