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
 }
}

猜你喜欢

转载自www.cnblogs.com/frankcui/p/10810663.html
今日推荐