Java基础-instanceof

package com.hspedu.instanceof_;

import org.junit.Test;

public class TestInstanceof {

@Test
public void testInstanceof() {

Son son = new Son();
System.out.println(son instanceof Son); // true
System.out.println(son instanceof Father); // true

Father father = new Son();
System.out.println(father instanceof Father); // true
System.out.println(father instanceof Son); // true
System.out.println(father instanceof Object); // true
}
}


class Father {

}

class Son extends Father {

}

instanceof是判断对象的运行类型是否是改类,或其子类

posted @ 2022-05-09 16:32  柯南同学  阅读(22)  评论(0编辑  收藏  举报