Java review(10) instanceof运算符 及 多态浅谈
- instanceof故名思意:instance是实例,of是谁的,因此instanceof翻译为“xx是xx的实例”,它用于类型判断,语法是
对象 instanceOf 类名
,返回true或false。
@Test
public void test1() throws Exception {
Object a = 1;
// 类型判断不能用在基本类型上
// System.out.println(a instanceof int);
// System.out.println(1 instanceof Integer);
// 正确的是:
System.out.println(a instanceof Integer);
}
输出:
true
可以通过这种方式判断类型,传入的可以是任意一个对象:
private void check(Object o) {
if (o instanceof Integer) {
System.out.println("o instanceof Integer");
}
if (o instanceof Short) {
System.out.println("o instanceof Short");
}
if (o instanceof Float) {
System.out.println("o instanceof Float");
}
if (o instanceof Double) {
System.out.println("o instanceof Double");
}
if (o instanceof Character) {
System.out.println("o instanceof Character");
}
if (o instanceof String) {
System.out.println("o instanceof String");
}
if (o instanceof Boolean) {
System.out.println("o instanceof Boolean");
}
if (o instanceof Object) {
System.out.println("o instanceof Object");
}
}
null 找不到 instanceof的对象,null不是Object,
instanceof null 是错误的
@Test public void test2() throws Exception { Boolean b = null; // null 找不到 instanceof的对象,null不是Object // instanceOf null 是错误的 check(b); }
以下对多态的理解十分重要,其中类Tb继承了Ta,实现了Ti接口,而Tc只实现了Ti接口,我们要判断的就是,子类 instanceof 父类,子类转型父类后:父类 instanceof 子类,父类实例 instanceof 子类:
class Ta {}
interface Ti {}
class Tb extends Ta implements Ti {}
class Tc implements Ti {}
@Test
public void test4() throws Exception {
Tb tb = new Tb();
Tc tc = new Tc();
System.out.println("tb instance of Tb: " + (tb instanceof Tb));
System.out.println("tb instance of Ta: " + (tb instanceof Ta));
System.out.println("tb instance of Ti: " + (tb instanceof Ti));
System.out.println("tc instance of Ti: " + (tc instanceof Ti));
System.out.println("----------------------------------");
// 向上转型后
Ta ta = tb;
System.out.println("ta instance of Tb: " + (ta instanceof Tb));
System.out.println("ta instance of Ta: " + (ta instanceof Ta));
System.out.println("----------------------------------");
// 接口转型
Ti ti = tb;
System.out.println("ti instance of Ta: " + (ti instanceof Ta));
System.out.println("ti instance of Tb: " + (ti instanceof Tb));
System.out.println("ti instance of Tc: " + (ti instanceof Tc));
System.out.println("ti instance of Ti: " + (ti instanceof Ti));
System.out.println("----------------------------------");
ti = tc;
System.out.println("ti instance of Ta: " + (ti instanceof Ta));
System.out.println("ti instance of Tb: " + (ti instanceof Tb));
System.out.println("ti instance of Tc: " + (ti instanceof Tc));
System.out.println("ti instance of Ti: " + (ti instanceof Ti));
System.out.println("----------------------------------");
// 父类实例
ta = new Ta();
System.out.println("ta instance of Tb: " + (ta instanceof Tb));
System.out.println("ta instance of Ta: " + (ta instanceof Ta));
}
tb instance of Tb: true
tb instance of Ta: true
tb instance of Ti: true
tc instance of Ti: true
----------------------------------
ta instance of Tb: true
ta instance of Ta: true
----------------------------------
ti instance of Ta: true
ti instance of Tb: true
ti instance of Tc: false
ti instance of Ti: true
----------------------------------
ti instance of Ta: false
ti instance of Tb: false
ti instance of Tc: true
ti instance of Ti: true
----------------------------------
ta instance of Tb: false
ta instance of Ta: true
以上结果证明了:
- 子类 instanceof 父类、子类 instanceof 接口恒为 true
- 子类实例转型为父类、接口时,其类型会保留,此时父类引用 instanceof 子类仍为 true
- 子类实例之间类型不同,互相instanceof 总为false
- 父类的实例 instanceof 子类总为false,(循环 继承 是不行滴,所以这句话没有问题)
一句话总结是
母鸡是鸡(1),母鸡是母的鸡(2),母鸡不是公鸡(3),鸡不一定是母鸡(4)
版权声明
本文章由作者“衡于墨”创作,转载请注明出处,未经允许禁止用于商业用途
发布时间:2019年11月28日 17:20:06
备案号:
闽ICP备19015193号-1
关闭特效
评论区#
还没有评论哦,期待您的评论!
引用发言