小编use*_*225的帖子

铸造会影响instanceof操作的结果吗?

我糊涂了.

假设我们有以下课程:

class Shape { /* ... */ }
class Square extends Shape { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

什么是由此产生的布尔值,为什么会这样呢?

Shape shape = ...;
boolean b1 = shape instanceof Square;

Square square = ...;
boolean b2 = ((Shape) square) instanceof Square;

boolean b3 = shape instanceof Object;
Run Code Online (Sandbox Code Playgroud)

据我所知,子类是父类的实例,但不是相反?

java object instanceof

2
推荐指数
1
解决办法
363
查看次数

标签 统计

instanceof ×1

java ×1

object ×1