1 / 72

class Point { }

class Point { }. class Point { }. Point. class Point { int x, y; }. Point. x. y. class Point { int x, y; }. Point. x. y. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } }. Point. x. y. Point. x. y.

natasha
Télécharger la présentation

class Point { }

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. class Point { }

  2. class Point { } Point

  3. class Point { int x, y; } Point x y

  4. class Point { int x, y; } Point x y

  5. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y

  6. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y This is a constructor, it looks just like a method (procedure, recipe), its purpose is just to describe the procedure for object initialization.

  7. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x; this.y = y;

  8. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x; this.y = y;

  9. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x; this.y = y;

  10. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y

  11. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point

  12. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point Sometime in the future, in another program:

  13. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point Sometime in the future, in another program: Point a = new Point(3, 5);

  14. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point Here how it goes. Point a = new Point(3, 5);

  15. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y First the blueprint is brought. Point a = new Point(3, 5);

  16. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y We’ll use it to create an actual object. Point a = new Point(3, 5);

  17. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y Someone will have to work on that. Point a = new Point(3, 5);

  18. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y Here’s the action figure. This is our employee. Point a = new Point(3, 5);

  19. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y He jumps right in. He works on the spaceship. Point a = new Point(3, 5);

  20. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y this.x = x this.y = y The arguments he received are 3 and 5. Point a = new Point(3, 5);

  21. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 x y this.x = x this.y = y The arguments he received are 3 and 5. Point a = new Point(3, 5);

  22. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y this.x = x this.y = y The arguments he received are 3 and 5. Point a = new Point(3, 5);

  23. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y this.x = x this.y = y So the employee reads the first line, and thinks… Point a = new Point(3, 5);

  24. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y this.x = x this.y = y this.x (the x in this spaceship) must be set to… Point a = new Point(3, 5);

  25. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y this.x = x this.y = y this.x (the x in this spaceship) must be set to… x Point a = new Point(3, 5);

  26. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y this.x = x this.y = y this.x (the x in this spaceship) must be set to… 3 Point a = new Point(3, 5);

  27. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y 3 this.x = x this.y = y so he takes the value in x in his left hand (3). Point a = new Point(3, 5);

  28. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point 3 5 x y 3 this.x = x this.y = y and places it in its right hand (the 3) Point a = new Point(3, 5);

  29. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x this.y = y and places it in this spaceship’s x slot… Point a = new Point(3, 5);

  30. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x this.y = y and places it in this spaceship’s x slot… a slam dunk! Point a = new Point(3, 5);

  31. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x this.y = y then moves on to second instruction in his program Point a = new Point(3, 5);

  32. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x this.y = y it reads: this.y = y; Point a = new Point(3, 5);

  33. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x this.y = y Spiff says: `That ought to be easy!’ Point a = new Point(3, 5);

  34. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x 5 this.y = y With left hand he takes the y provided by the user… Point a = new Point(3, 5);

  35. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 y Point 3 5 x y this.x = x 5 this.y = y …and moves it in his right hand… Point a = new Point(3, 5);

  36. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y Point 3 5 x y this.x = x this.y = y For another slam dunk (I guess). Point a = new Point(3, 5);

  37. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y Point 3 5 x y this.x = x this.y = y Spiff’s job is done. Point a = new Point(3, 5);

  38. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y He packs and leaves behind a full blown object. Point a = new Point(3, 5);

  39. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y The object is completely initialized… Point a = new Point(3, 5);

  40. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y … and must be delivered to the customer. Point a = new Point(3, 5);

  41. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y Spiff is happy. Spiff can go away. Point a = new Point(3, 5);

  42. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y Spiff is off to help another customer. a

  43. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y When a new order comes: Point b = new Point(-1, -1); a

  44. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y x -1 -1 y When a new order comes: Point b = new Point(-1, -1); a

  45. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y x -1 -1 y b It’s déjà vu already again… a

  46. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x 3 5 y x -1 -1 y b a

  47. So that’s how constructors work…

  48. Let’s see how instance methods behave…

  49. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point x y

  50. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point x y Point Let’s ignore the variable(s) initialization details for now…

More Related