绍兴哪些公司做网站,网页建设的公司,国内十强少儿编程机构,潮流设计网站Java中#xff0c;instanceof运算符的前一个操作符是一个引用变量#xff0c;后一个操作数通常是一个类(可以是接口)#xff0c;用于判断前面的对象是否是后面的类#xff0c;或者其子类、实现类的实例。如果是返回true#xff0c;否则返回false。也就是说#xff1a;使用…Java中instanceof运算符的前一个操作符是一个引用变量后一个操作数通常是一个类(可以是接口)用于判断前面的对象是否是后面的类或者其子类、实现类的实例。如果是返回true否则返回false。也就是说使用instanceof关键字做判断时 instanceof 操作符的左右操作数必须有继承或实现关系下面我们用继承树来判断instanceof的返回值1 packageinstanceofTest;23 /**4 *authorzsh5 * company wlgzs6 * create 2019-03-21 19:557 * Describe 继承树来判断instanceof的返回值8 */9 interfaceMan{}10 class Person1 implementsMan{}11 class Student extendsPerson1{}12 class Postgraduate extendsStudent {}13 classAnimal {}14 public classMain1 {15 public static voidmain(String[] args) {16 System.out.println(Student 的对象是谁的实例);17 instanceofTest(newStudent());18 System.out.println(Animal 的对象是谁的实例);19 instanceofTest(newAnimal());20 System.out.println(Postgraduate 的对象是谁的实例);21 instanceofTest(newPostgraduate());22 //一个类的实例是这个类本身的实例也是他父类父类的父类的实例也是实现的接口的实例23 }2425 public static voidinstanceofTest(Object p) {26 if (p instanceofAnimal)27 System.out.println(p.getClass() 类的实例 是类Animal的实例);28 if (p instanceofPostgraduate)29 System.out.println(p.getClass() 类的实例 是类Postgraduate的实例);30 if (p instanceofStudent)31 System.out.println(p.getClass() 类的实例 是类Student的实例);32 if (p instanceofPerson1)33 System.out.println(p.getClass() 类的实例 是类Person的实例);34 if (p instanceofMan)35 System.out.println(p.getClass() 类的实例 是接口Man的实例);36 if (p instanceofObject)37 System.out.println(p.getClass() 类的实例 是类Object的实例);38 }39 }上面的程序展示各类之间的关系的继承树是上述程序中由上面继承树可知某个类(接口也可以看成一个特殊的类)的对象是不是其他类(或接口)的实例只需按箭头方向以此对象所在的类为起点到达此继承树分支(可能有多个分支)终点沿途经过的类(包括本类或接口)就都是该对象的实例。所以输出结果是但是要注意一点在判断某个类(接口也可以看成一个特殊的类)的对象是不是其他类(或接口)的实例一定要首先进行向上转型然后才可用instanceof关键字进行判断这是基本操作规范。如1 packageinstanceofTest;23 /**4 *authorzsh5 * company wlgzs6 * create 2019-03-21 20:027 * Describe8 */910 interfaceA{11 voidsay();12 }1314 class B implementsA{15 public voidsay()16 {17 System.out.println(B实现的say()方法);18 }19 }2021 class C implementsA{22 public voidsay()23 {24 System.out.println(C实现的say()方法);25 }26 }2728 public classMain2 {29 public static voidmain(String[] args) {30 A a new B(); //接口不能new31 System.out.println(a instanceof B); //true;发生了A a new B();32 System.out.println(a instanceof C); //false;没有发生A a new C();33 }34 }运行结果以上各类的之间关系的继承树如下在判断接口A的对象a 是不是类C的实例时没有先进行向上转型就进行instanceof关键字的使用了是肯定会返回false的。测试用例1 packageinstanceofTest;23 /**4 *authorzsh5 * company wlgzs6 * create 2019-03-21 20:077 * Describe instanceof 测试用例8 */9 interfaceA{1011 }12 class B implementsA{1314 }15 class C extendsB{1617 }18 public classMain3 {19 public static voidmain(String[] args) {20 A abnewB();21 A acnewC();22 B bcnewC();23 B bbnewB();24 C ccnewC();25 //对象实现一个接口用这个对象和这个接口进行instanceof判断都为true。26 System.out.println(ab instanceof A(ab instanceofA));27 System.out.println(ac instanceof A(ac instanceofA));28 System.out.println(bc instanceof A(bc instanceofA));29 System.out.println(bb instanceof A(bb instanceofA));30 System.out.println(cc instanceof A(cc instanceofA));31 //对象和父类进行instanceof判断都为true32 System.out.println(ab instanceof B(ab instanceofB));33 System.out.println(ac instanceof B(ac instanceofB));34 System.out.println(bc instanceof B(bc instanceofB));35 System.out.println(bb instanceof B(bb instanceofB));36 System.out.println(cc instanceof B(cc instanceofB));37 //对象和他的子类进行instanceof判断为false38 System.out.println(ab instanceof C(ab instanceofC));39 System.out.println(ac instanceof C(ac instanceofC));40 System.out.println(bc instanceof C(bc instanceofC));41 System.out.println(bb instanceof C(bb instanceofC));42 System.out.println(cc instanceof C(cc instanceofC));43 }44 }运行结果总结如果一个类的实例是这个类本身的实例那么它也是它的父类、它的父类的父类的实例也是由它实现的接口的实例且instanceof左边操作元显式声明的类型与右边操作元必须是同种类或右边是左边父类的继承关系此外//false;这是instanceof 特 有 的 规 则 若左操作数为null 结果就直接返回false 不再运算右操作数是什么类。boolean b5 null instanceof String;//编译不通过;A在此处视为基本数据类型charinstanceof操作符只能用作对象的判断boolean b4 A instanceof Character;