Java类型检查
Java类型转换前一般有三种方式进行类型检查
- isAssignableFrom
- isInstance
- instanceof
isAssignableFrom
X.class.isAssignableFrom(Y.class)
If X and Y are the same class, or X is Y’s super class or super interface, return true, otherwise, false
isInstance
X.class.isInstance(y)
Say y is an instance of class Y, if X and Y are the same class, or X is Y’s super class or super interface, return true, otherwise, false.
instanceof
x instanceof X
For instanceof you need to know the exact class X at compile time.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 石头记!