//亲子鉴定 instanceof
/*已知条件:
动物类是dog类,mao类,people类的父类
Object是所有类的祖宗类,String是祖宗类的其中一个类
Object-->动物-->dog,mao,people
*/
package com.oop.多态另一种表现;
public class QZJD {
public static void main(String[] args) {
dog dog = new dog();
System.out.println(dog instanceof dog);//ture
System.out.println(dog instanceof 动物);//ture
System.out.println(dog instanceof Object);//ture
System.out.println("=======");
Object object = new dog();
System.out.println(object instanceof dog);//ture
System.out.println(object instanceof 动物);//ture
System.out.println(object instanceof mao);//false
System.out.println(object instanceof people);//false
System.out.println("========");
动物 dw = new dog();
System.out.println(dw instanceof dog);//ture
System.out.println(dw instanceof 动物);//ture
System.out.println(dw instanceof Object);//ture
System.out.println(dw instanceof mao);//false
System.out.println(dw instanceof people);//false
//System.out.println(dw instanceof String);
//因为dw(动物类)与String类没有任何关系所以打不出来这个判断词
System.out.println("==========");
//mao mao= new dog();//同类的引用不可以指向同类
mao mao = new mao();//本类的引用可以指向本类
/*
总结:
instanceof就是一个判断词
1.首先判断A与B有没有关系
2.再判断B与类名2是不是为父子关系或者本类关系,如果是那么就输出ture
A instanceof B
类名1 A=new 类名2();
*/
}
}
控制台输出结果
标签:instanceof,Demo59,dog,多态,System,println,mao,out From: https://www.cnblogs.com/CHX249/p/16861461.html