360 likes | 464 Views
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){
E N D
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){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } }
Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 1
Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 2
Parameters public class Library{ public static void main(String [] args){ Book book1 = new Book(“John Fish”, “Die Hard”, 25.69); } } Actual Parameter 3
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
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
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
Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 13); } }
Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 13); } } Is the 3rd actual parameter legal?
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
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?
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
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
Parameters public class Library{ public static void main(String [] args){ Book book2 = new Book(“J. Cox”, “Jaws”, 62); } } Is the 3rd actual parameter legal?
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
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?
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
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
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!
Method Returns • void go(){ • }
Method Returns • int go(){ • return 5; • }
Method Returns • int go(){ • int x = 5; • return x; • }
Method Returns • String go(){ • String x = “hello buddy”; • return x; • }
Method Returns • long go(){ • int x = 5; • return x; • }
Method Returns • byte go(){ • short x = 5; • return x; • } 불범! implicit narrowingconversion does not work for method returns
Method Returns • Dog go(){ • Dog x = new Dog(); • return x; • }
Encapsulation class Soju { int 술; }
Encapsulation class Soju { int 술; } class SojuTestDrive { public static void main(String[] args){ Soju ohMyGod = new Soju(); ohMyGod.술 = 90; } } What? 90% 술?
Encapsulation class Soju { int 술; }
Encapsulation class Soju { private int 술; }
Encapsulation class Soju { private int 술; void set술(int x){ 술 = x; } } setter method
Encapsulation class Soju { private int 술; void set술(int x){ if(x<45){ 술 = x; } else{ System.out.print(“안돼!”); } } }
Encapsulation class SojuTestDrive { public static void main(String[] args){ Soju ohMyGod = new Soju(); ohMyGod.set술(90); } } 안돼!
Encapsulation class Soju { private int 술; void set술(int x){ … } int get술(){ return 술; } } getter method
Encapsulation class SojuTestDrive { public static void main(String[] args){ Soju ok = new Soju(); ok.set술(20); System.out.print(ok.get술()); } }