1 / 15

4. 객체의 행동

4. 객체의 행동. 객체의 상태와 행동. Song. title artist. 인스턴스 변수 ( 상태 ). setTitle setArtist () play(). 메소드 ( 행동 ). My Way Sex Pistols. Sing Travis. Poltik Coldplay. My Way Sivatra. s3. t2. Song s3.play();. Song t2.play();. 크기와 짖는 소리 (1/2). Dog. class Dog { int size ;

hamlet
Download Presentation

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. 4. 객체의 행동

  2. 객체의 상태와 행동 Song title artist 인스턴스 변수(상태) setTitle setArtist() play() 메소드 (행동) My Way Sex Pistols Sing Travis Poltik Coldplay My Way Sivatra s3 t2 Song s3.play(); Song t2.play(); Head First JAVA

  3. 크기와 짖는 소리 (1/2) Dog classDog { intsize; String name; voidbark () { if (size > 60) { System.out.println(“Wooof! Wooof!"); } else if (size > 14) { System.out.println(“Ruff! Ruff!"); } else { System.out.println(“Yip! Yip!"); } } } Dog size name bark() Head First JAVA

  4. 크기와 짖는 소리 (2/2) DogTestDrive classDogTestDrive { public static void main (String [] args) { Dog one = new Dog (); one.size = 70; Dog two = new Dog (); two.size = 8; Dog three = new Dog (); three.size = 35; one.bark(); two.bark(); three.bark(); } } % java DogTestDrive Wooof! Wooof! Yip! Yip! Ruff! Ruff! Head First JAVA

  5. 메소드 매개변수 1. Dog 레퍼런스의bark 메소드를 호출. 이 때 3(인자)이라는 값을 전달 Dog d = new Dog(); d.bark(3); 2. bark 메소드에는3을 나타내는 비트들이 전달 00000011 3. 비트들은 numOfBarks (매개변수)에 들어감 void bark (intnumOfBarks) { while (numOfBarks > 0) { System.out.println(“ruff”); numOfBarks = numOfBarks – 1; } } 4. 매개변수는 메소드 코드 내에서 변수로 쓰임 Head First JAVA

  6. return void go() { } 리턴 유형을 void로 선언하면 아무것도 리턴하지 않음 intgiveSecret () { return 42; } inttheSecret = life.giveSecret (); giveSecret() 메소드에서는42를나타내는 비트들을 리턴하며그 값이 theSecret이라는 이름을 가진 변수로 들어감 이 두 유형은 반드시 같게 00101010 Head First JAVA

  7. 인자 여러 개 (1/2) void go () { TestStuff t = newTestStuff (); t. takeTwo (12, 34); } voidtakeTwo (int x, int y) { int z = x + y; System.out.println (“Total is “ + z); { 이렇게 인자 두 개를 보내면 그 두 인자는 보낸 순서대로 들어감 Head First JAVA

  8. 인자 여러 개 (2/2) 변수 유형이 매개변수 유형과 일치한다면 변수를 매개변수로 전달할 수도 있음 void go () { intfoo = 7; int bar = 3; t. takeTwo (foo, bar); } voidtakeTwo (int x, int y) { int z = x + y; System.out.println (“Total is “ + z); { 이 때 자바에서는 값(복사본)으로 전달 Head First JAVA

  9. 핵심정리 (1/2) 클래스에서는 객체가 아는 것과 객체가 하는 것을 정의한다. 메소드(행동)는객체가 하는 것이다. 메소드에서인스턴스 변수를 이용하여 같은 유형의 객체가 다른 식으로 행동하게 할 수 있다. 메소드에서매개변수를 사용할 수 있다. 즉 메소드에 값 한 개 이상을 전달할 수 있다. 전달하는 값의 개수와 유형은 반드시 메소드를 선언할 때 지정한 것과 같아야 하며 그 순서도 같아야 한다. 인스턴스 변수(상태)는 객체가 아는 것이다. Head First JAVA

  10. 핵심정리 (2/2) 메소드 안팎으로 전달되는 값은 상황에 따라 자동으로 더 큰 유형으로 올라갈 수 있다. 더 작은 유형으로 바꿔야 한다면 강제로 캐스팅을 해야 함 메소드에 인자를 전달할 때는 리터럴값을 사용할 수 있고 선언된 매개변수 유형의 변수를 사용할 수도 있다. 메소드를 선언할 때 반드시 리턴 유형을 지정해야 한다. 리턴 유형을 void로 지정하면 아무것도 리턴하지 않아도 된다. 메소드를 선언할 때 void가 아닌 리턴 유형을 지정했을 때는 반드시 선언된 리턴 유형과 호환 가능한 값을 리턴해야 한다. Head First JAVA

  11. Getter 와Setter classElectricGuitar { String brand; intnumOfPickups; booleanrockStarUsesIt; String getBrand() { return brand; } void setBrand(String aBrand) { brand= aBrand; } intgetNumOfPickups() { return numOfPickups; } void setNumOfPickups(int num) { numOfPickups= num; } booleangetRockStarUsesIt() { return rockStarUsesIt; } void setRockStarUsesIt(booleanyesOrNo) { rockStarUsesIt= yesOrNo; } } 게터는 단지 그 게터가 가져오기로 되어있는 값을 리턴값 형대로 받아오기 위한 용도 세터는 그 세터가 설정한 값을 인자로 받아서 인스턴스 변수를 그 값으로 설정하기 위한 용도 Head First JAVA

  12. 클래스 캡슐화 (1/2) 인스턴스 변수는 private으로.. 게터와 세터는 public으로.. classGoodDog { private intsize; public intgetSize() { returnsize; } public void setSize(int s) { size = s; } voidbark() { if (size > 60) { System.out.println("Wooof! Wooof!"); } else if (size > 14) { System.out.println("Ruff! Ruff!"); } else { System.out.println("Yip! Yip!"); } } } Dog size getSize() setSize() bark() Head First JAVA

  13. 클래스 캡슐화 (2/2) classGoodDogTestDrive { public static void main (String [] args) { GoodDogone = newGoodDog(); one.setSize(70); GoodDogtwo = newGoodDog(); two.setSize(8); System.out.println("Dog one: " + one.getSize()); System.out.println("Dog two: " + two.getSize()); one.bark(); two.bark(); } } 캡슐화를 사용하면 다른 코드에서 항상 세터(게터) 메소드를 사용하게 하여 뭔가를 변경해도 다른 코드에는 피해 없게 함 Head First JAVA

  14. 인스턴스 변수와 지역 변수 인스턴스 변수는 클래스 내에서 선언된다. 인스턴스 변수에는 항상 어떤 기본값이 들어감. 인스턴스 변수에 직접 어떤 값을 대입하거나 세터 메소드를 호출하지 않은 경우에도 그 인스턴스 변수에는 기본값이 있음. 정수 0 부동소수점 소수 0.0 부울false 레퍼런스null 지역변수는 메소드 내에서 선언된다. 지역변수는 사용하기 전에 반드시 초기화 해야 한다. Head First JAVA

  15. 변수 비교 int a = 3; byte b = 3; if ( a == b ) { // 참 } 두 원시 값을 비교할 때는 == 연산자를 사용 Foo a = new Foo(); Foo b = new Foo(); Foo c = a; if ( a == b ) { // 거짓 } if ( a == c ) { // 참 } if ( b == c ) { // 거짓 } 레퍼런스 두 개가 똑같은지 확인할 때도 == 연산자를 쓸 수 있음 Head First JAVA

More Related