Examples of abnormal behavior in Java
•
Java
Can anyone explain why the following if statement evaluates to false?
public void addShapeToWhiteboard(PolyLine shape) { Window.alert("2"); if(shape instanceof PolyLine) { Window.alert("3"); this.whiteboard.add((PolyLine)shape); Window.alert("3.5"); } this.whiteboard.draw(); Window.alert("4"); }
It needs a "polyline" object, but instanceof returns false because I receive an alarm of "2", then an alarm of "4", and there is no clue, or even possible
Solution
Maybe the shape is empty? In this case, instanceof returns false
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码