当前位置:网站首页>Example description of alternative writing of instanceof
Example description of alternative writing of instanceof
2022-07-19 08:42:00 【qq_ twenty-five million seventy-three thousand two hundred and 】
Reference material :
instanceOf Alternative writing of method -- Have you ever seen ?
instanceof brief introduction :
instanceof yes Java、php A binary operator of ( Operator ), and ==,>,< It's the same thing . Because it's made up of letters , So too. Java Reserved keywords for . Its function is to determine whether the object on the left is an instance of the class on the right , return boolean Data of type . It can be used to judge whether the instance of the subclass in the inheritance is the implementation of the parent class . amount to c# Medium is The operator .java Medium instanceof Is to indicate by returning a Boolean value , Whether this object is an instance of this specific class or its subclass .
instanceof yes Java Reserved keywords for , Its purpose is to test whether the object on its left is an instance of the class on its right , return boolean Data type of
matters needing attention :
instanceof It can't be used to judge basic types , It can only be used to judge the reference type
instanceof Application scenarios of
instanceof Keywords are often used to determine the type before casting , When the judgment is finished , Then follow up the corresponding conversion operation
public B convert(A a) {
if (a instanceof B) {
return (B) a;
}
return null;
}
instanceof Implementation principle of
Use try catch Enforce , Conversion success , Then return to true, Instead, return to false
boolean rs;
if (obj == null) {
rs= false;
} else {
try {
T temp = (T) obj;
rs= true;
} catch (ClassCastException e) {
rs = false;
}
}
return rs;
example :
instanceof Use example of
boolean result = object instanceof class
Parameter description :
Result: Boolean type .
Object: Will options . Any object expression .
class: Will options . Any defined object class .
explain
When object yes class An example of
be instanceof Operator return true
If object Not an instance of the specified class
or object yes null, Then return to false.instanceof Alternative writing use class One of the objects isInstance Method , As shown below
if (classname.class.isInstance(othername)){
// Business code
}边栏推荐
- Change the theme of hbuilderx into vscode
- 65. Restful specification
- Softmax regression + loss function + image classification dataset
- 深度学习第一周Introduction to Deep Learning习题整理
- ROS常用工具包命令
- 6-9 vulnerability exploitation telnet login rights lifting
- MySQL8下的JSON
- (鬼火引擎)中多设备的支持
- 百度Apoll
- LeetCode 剑指 Offer II 041. 滑动窗口的平均值:低空间消耗解决
猜你喜欢

Basic steps for creating a static library

Hand in hand practice a DAPP, the road to Web3.0!

idea按条件断点调试

Junit5

Cbcgpedit control used by BCG

Change the theme of hbuilderx into vscode

matlab导入小数点后9位以上的浮点数

Database write Optimization: database and table segmentation and related issues

How to position the circle of friends? Three core steps to build a circle of friends selling popular products

Stm32subeide (9) -- USART sends and receives via DMA
随机推荐
Redis 概述安装
49. MySQL usage
trochvision中数据集的使用
Nacos new configuration management
Unity: window size adaptation when running on the browser after webgl Publishing
Softmax 回归 + 损失函数 + 图片分类数据集
凸面镜面3D玻璃轮廓扫描
Yyds dry inventory cross origin cross domain request
Introduction to deep learning exercises sorting in the first week of deep learning
巴西移动游戏代投出海机遇与挑战
Li Kou 43 string multiplication note
Translucent double glass side thickness
5g at that time, where will driverless driving go in the future?
分布式事务-最大努力通知方案
Redis introduction
Finishing of key concepts on deep neural networks exercises in the fourth week of in-depth learning
scratch逆序输出 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
Error received from peer ipv4/connection reset by peer paddleserving
Stm32subeide (9) -- USART sends and receives via DMA
Eureka Basics