instanceof:
- 判断某个对象是否属于某种数据类型
- 只能判断继承关系下的
- 多态对象时,引用对象时可识别同级关系
父类 对象 = new 子类();
案例
package com.zking.qiao;
/**
* 父类 动物类
* @author Administrator
*
*/
public class Animal {
}
package com.zking.qiao;
/**
* 子类 猫类
* @author Administrator
*
*/
public class Cat extends Animal{
}
package com.zking.qiao;
/**
* 子类 狗类
* @author Administrator
*
*/
public class Dog extends Animal{
}
测试
package com.zking.qiao;
public class TestAni {
public static void main(String[] args) {
//创建对象
Animal a = new Dog();
Cat c = new Cat();
Dog d = new Dog();
//判断 instanceof
System.out.println(c instanceof Animal);//猫继承于动物
System.out.println(a instanceof Cat);//不明确对象,同级子类皆可识别
System.out.println(a instanceof Dog);
}
}