1 / 2

i nstanceof

i nstanceof. x instanceof Y returns true if object x is an instance of class Y Example (but better example on next panel) public void moveTo (Point2D point) { if (point instanceof Point2D.Double) { // do stuff that relies on double-precision accuracy } else {

ros
Download Presentation

i nstanceof

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. instanceof • x instanceof Y • returns true if object x is an instance of class Y • Example (but better example on next panel) public void moveTo(Point2D point) { if (point instanceof Point2D.Double) { // do stuff that relies on double-precision accuracy } else { // only rely on single-precision accuracy } }

  2. Casting • Example: public Dud(BallEnvironment be) { if (be instanceofWorld) { ((World) (be)).STUFF_THAT_WORLDS_CAN_DO... } } • Note weird notation: (Class) (thing-to-cast) • You will need this notation for implementing equals in BigRational

More Related