Java 基础 - instanceof关键字
Posted frankcui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 基础 - instanceof关键字相关的知识,希望对你有一定的参考价值。
instanceof 父类子类
结论:
- 子类 instanceof 父类 == true
- 父类 instanceof 子类 == false
public class Test { public static void main(String[] args) { //instanceof 父类子类 Father father1 = new Father(); System.out.println(father1 instanceof Father ); //ture System.out.println(father1 instanceof Son ); //false Father father2 = new Son(); System.out.println(father2 instanceof Father ); //ture System.out.println(father2 instanceof Son ); //ture Son son = new Son(); System.out.println(son instanceof Father ); //ture System.out.println(son instanceof Son ); //ture } }
以上是关于Java 基础 - instanceof关键字的主要内容,如果未能解决你的问题,请参考以下文章