提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
具有类层次结构的运算符的instanceof
/** 时 代 J a v a 公 众 号 - nowjava.com 提供 **/ class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: "// 来自 n o w j a v a . c o m - 时 代 Java + (obj2 instanceof MyInterface)); } } class Parent { } class Child extends Parent implements MyInterface { } interface MyInterface { }