announcements
play

Announcements Lab 03 inBase late submissions Midterm #1 Friday - PowerPoint PPT Presentation

Announcements Lab 03 inBase late submissions Midterm #1 Friday Covers everything through basics of OOP Project 2 is posted Due next Wednesday All about nested loops good exam practice Coding style is part of the


  1. Announcements • Lab 03 inBase – late submissions • Midterm #1 Friday – Covers everything through basics of OOP • Project 2 is posted – Due next Wednesday – All about nested loops – good exam practice – Coding style is part of the grade • Evan Golub’s slides • Avoiding Redundant code

  2. Basics of Object Oriented Programming Cont’d

  3. The keyword Class Refers to whichever object is currently executing the instruction Memory Object 01 Object 02 Object 03 x x x 3 -1 101 … … … … … …

  4. Execution flow public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } }

  5. Execution flow public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } }

  6. Execution flow //A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3); obj1.grow(3); obj2.grow(3); obj1.gift(obj2); }

  7. Execution flow //A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … } obj1 data

  8. Execution flow //A main method somewhere else public static void main(String[] args) { MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … } dat 1 obj1 data

  9. Execution flow public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … dat 1 } obj1 data

  10. Execution flow public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … dat 1 } obj1 data 1 this.data

  11. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … } obj1 data 1

  12. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … } obj1 obj2 data 1 data

  13. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … dat 2 } obj1 obj2 data 1 data

  14. Execution flow public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … dat 2 } obj1 obj2 1 data data

  15. Execution flow public class MyObj { //A sample class definition … public MyObj(int dat) { //Constructor this.data = dat; } … dat 2 } obj1 obj2 data 1 data 2 this.data

  16. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj1 = new MyObj(1); MyObj obj2 = new MyObj(2); … } obj1 obj2 data 1 data 2

  17. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3); … } obj1 obj2 data 1 data 2

  18. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj2 = new MyObj(2); MyObj obj3 = new MyObj(3); … } obj1 obj2 obj3 data 1 data 2 data 3

  19. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3); obj1.grow(3); … more 3 } obj1 obj2 obj3 data 1 data 2 data 3

  20. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3); obj1.grow(3); … more 3 } obj1 obj2 obj3 data 1 data 2 data 3

  21. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 1 data 2 data 3

  22. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 1 data 2 data 3 this.data

  23. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 4 data 2 data 3 this.data

  24. Execution flow //A main method somewhere else public static void main(String[] args) { … MyObj obj3 = new MyObj(3); obj1.grow(3); … } obj1 obj2 obj3 data 4 data 2 data 3

  25. Execution flow //A main method somewhere else public static void main(String[] args) { … obj1.grow(3); obj2.grow(3); … more 3 } obj1 obj2 obj3 data 4 data 2 data 3

  26. Execution flow //A main method somewhere else public static void main(String[] args) { … obj1.grow(3); obj2.grow(3); … more 3 } obj1 obj2 obj3 data 4 data 2 data 3

  27. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 4 data 2 data 3

  28. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 4 data 2 data 3 this.data

  29. Execution flow public class MyObj { … public void grow(int more) { this.data = this.data + more; } … more 3 } obj1 obj2 obj3 data 4 data 5 data 3 this.data

  30. Execution flow //A main method somewhere else public static void main(String[] args) { … obj1.grow(3); obj2.grow(3); … } obj1 obj2 obj3 data 4 data 5 data 3

  31. Execution flow //A main method somewhere else public static void main(String[] args) { … obj2.grow(3); obj1.gift(obj2); … other obj2 } obj1 obj2 obj3 data 4 data 5 data 3

  32. Execution flow //A main method somewhere else public static void main(String[] args) { … obj2.grow(3); obj1.gift(obj2); … other obj2 } obj1 obj2 obj3 data 4 data 5 data 3

  33. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 5 data 3

  34. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 5 data 3

  35. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 5 data 3 other.data

  36. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 5 data 3 this .data

  37. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 9 data 3 this .data

  38. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 4 data 9 data 3 this .data

  39. Execution flow public class MyObj { … public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } other obj2 } obj1 obj2 obj3 data 0 data 9 data 3 this .data

  40. Execution flow //A main method somewhere else public static void main(String[] args) { … obj2.grow(3); obj1.gift(obj2); … } obj1 obj2 obj3 data 0 data 9 data 3

  41. Omit the this keyword? • If you reference an instance field or method, and omit the this keyword, Java assumes you are referring to the current object. • Standard practice is to omit this in all but a few special cases.

  42. Omit the this keyword? public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor this.data = dat; } //Instance methods public void grow(int more) { this.data = this.data + more; } public void gift(MyObj other) { other.data = other.data + this.data; this.data = 0; } }

  43. Omit the this keyword? public class MyObj { //A sample class definition int data; //instance field public MyObj(int dat) { //Constructor data = dat; } //Instance methods public void grow(int more) { data = data + more; } public void gift(MyObj other) { other.data = other.data + data; data = 0; } }

  44. When to use this • In a constructor: public MyObj(int dat) { //Constructor data = dat; } VS public MyObj(int data) { //Constructor this .data = data; } • Review shadowing (Lec04 – ArgsAndParams and example)

  45. When to use this • To return the object at the end. Compare: public void grow(int more) { data = data + more; } … //Somewhere else obj1.grow(3) System. out. println(obj1);

  46. When to use this • To return the object at the end. With: public MyObj grow(int more) { data = data + more; return this; } … //Somewhere else System. out. println(obj1.grow(3));

  47. A brief glance at access modifiers • Control whether objects of one type can access members in objects of another type. – public : Any object can access – private : Only objects of the same type can access • Gamble example – If bias is a private field in Coin, Gambler objects can’t access it.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend