栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中使用的“ instanceof”运算符是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中使用的“ instanceof”运算符是什么?

instanceofkeyword
是用于测试对象(实例)是否为给定Type的子类型的二进制运算符。

想像:

interface Domestic {}class Animal {}class Dog extends Animal implements Domestic {}class Cat extends Animal implements Domestic {}想象一个用创建的dog 对象Object dog = new Dog(),然后:dog instanceof Domestic // true - Dog implements Domesticdog instanceof Animal   // true - Dog extends Animaldog instanceof Dog      // true - Dog is Dogdog instanceof Object   // true - Object is the parent type of all objects

然而,随着

Object animal = new Animal()
;,

animal instanceof Dog // false

因为

Animal
是的超类型,
Dog
可能较少。

和,

dog instanceof Cat // does not even compile!

这是因为Dog既不是的子类型也不是的父类型Cat,并且它也不实现它。

请注意,

dog
上面用于的变量是类型
Object
。这是
instanceof
一个运行时操作,将我们带到一个用例:在运行时根据对象类型做出不同的反应。

注意事项:

expressionThatIsNull instanceof T
对于所有
Types
都是
false T



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号