1 / 36

Chapter 4

Chapter 4. Method parameters Formal and Actual parameters Method returns Encapsulation ‘private’ access modifier ‘public’ getters and setters Instance vs Local variables. Parameters. public class Library{ public static void main(String [] args){

michel
Télécharger la présentation

Chapter 4

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. Chapter 4 • Method parameters • Formal and Actual parameters • Method returns • Encapsulation • ‘private’ access modifier • ‘public’ getters and setters • Instance vs Local variables

  2. Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } }

  3. Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 1

  4. Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 2

  5. Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 3

  6. Parameters public class Book{ String author, title; double price; Book( String a, String b, double c ){ author = a; title = b; price = c; } } Formal Parameter 1

  7. Parameters public class Book{ String author, title; double price; Book( String a, String b, double c ){ author = a; title = b; price = c; } } Formal Parameter 2

  8. Parameters public class Book{ String author, title; double price; Book( String a, String b, double c ){ author = a; title = b; price = c; } } Formal Parameter 3

  9. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 13); } }

  10. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 13); } } Is the 3rd actual parameter legal?

  11. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 13); } } Is the 3rd actual parameter legal? Yes… implicit widening conversion: int > double

  12. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 2 * 14.1f / 3 - 1); } } Is the 3rd actual parameter legal?

  13. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 2 * 14.1f / 3 - 1); } } Is the 3rd actual parameter legal? Yes… expression is evaluated before passing

  14. Parameters public class Book{ String author, title; double price; Book( String a, String b, byte c ){ author = a; title = b; price = c; } } Change formal parameter to byte

  15. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 62); } } Is the 3rd actual parameter legal?

  16. Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 62); } } Is the 3rd actual parameter legal? No… implicit narrowing conversion DOES NOT WORK in method invocations

  17. Instance vs. Local variables public class Fun{ public static void main(String [] args){ inti = 0; addTwo( i++ ); System.out.println( i ); } static void addTwo( inti ){ i += 2; } } What is the output?

  18. Instance vs. Local variables public class Fun{ public static void main(String [] args){ inti = 0; addTwo( i++ ); System.out.println( i ); } static void addTwo( inti ){ i += 2; } } i != i main의 i = addTwo의 i

  19. Instance vs. Local variables public class Fun{ public static void main(String [] args){ inti = 0; addTwo( i++ ); System.out.println( i ); } static void addTwo( inti ){ i += 2; } } 1 ! Actual parameter and formal parameter are not the same variables

  20. public class ChefTestDrive{ public static void main(String[] args){ Chef me = new Chef(); me.bakeCookies( 5 ); me.bakeCookies( 2 ); me.makeGimchi( “hot” ); me.makeGimchi( “sweet” ); } } Write this in eclipse and add the Chef class Mmm. 5 delicious cookies Mmm. 2 delicious cookies Oh! Gimchi is too hot! Oh! Gimchi is too sweet!

  21. Method Returns • void go(){ • }

  22. Method Returns • int go(){ • return 5; • }

  23. Method Returns • int go(){ • int x = 5; • return x; • }

  24. Method Returns • String go(){ • String x = “hello buddy”; • return x; • }

  25. Method Returns • long go(){ • int x = 5; • return x; • }

  26. Method Returns • byte go(){ • short x = 5; • return x; • } 불범! implicit narrowingconversion does not work for method returns

  27. Method Returns • Dog go(){ • Dog x = new Dog(); • return x; • }

  28. Encapsulation class Soju { int 술; }

  29. Encapsulation class Soju { int 술; } class SojuTestDrive { public static void main(String[] args){ Soju ohMyGod = new Soju(); ohMyGod.술 = 90; } } What? 90% 술?

  30. Encapsulation class Soju { int 술; }

  31. Encapsulation class Soju { private int 술; }

  32. Encapsulation class Soju { private int 술; void set술(int x){ 술 = x; } } setter method

  33. Encapsulation class Soju { private int 술; void set술(int x){ if(x<45){ 술 = x; } else{ System.out.print(“안돼!”); } } }

  34. Encapsulation class SojuTestDrive { public static void main(String[] args){ Soju ohMyGod = new Soju(); ohMyGod.set술(90); } } 안돼!

  35. Encapsulation class Soju { private int 술; void set술(int x){ … } int get술(){ return 술; } } getter method

  36. Encapsulation class SojuTestDrive { public static void main(String[] args){ Soju ok = new Soju(); ok.set술(20); System.out.print(ok.get술()); } }

More Related