-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 16 : 클래스 안에 클래스가 들어갈 수도 있구나
공부 내용 정리 완료
- `Chapter 16 : 클래스 안에 클래스가 들어갈 수도 있구나` 공부 내용 정리 완료
- Loading branch information
Showing
9 changed files
with
654 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
### Chapter 16 : 클래스 안에 클래스가 들어갈 수도 있구나 | ||
|
||
- [`1. 클래스 안의 클래스`](./section_01_04.md#1-클래스-안의-클래스) | ||
- [`2. static nested 클래스의 특징`](./section_01_04.md#2-static-nested-클래스의-특징) | ||
- [`3. 내부 클래스와 익명 클래스`](./section_01_04.md#3-내부-클래스와-익명-클래스) | ||
- [`4. Nested 클래스의 특징은 꼭 알아야 한다`](./section_01_04.md#4-nested-클래스의-특징은-꼭-알아야-한다) | ||
|
||
--- | ||
|
||
### What I learned from this chapter | ||
|
||
이번 챕터에는 `Nested Class` 에 대해 배웠다. 중첩 클래스는 **잘** 만들면 코드의 가독성, 유지보수성을 높일 수 있는 좋은 전략이다. | ||
|
||
이러한 `Nested Class` 를 어렴풋이 알고 있었지만 이를 형식으로 갖추어 배운적은 처음이다. 그래서 큰 내용을 어렵지 않았지만 세부적으로 몰랐던 내용들이 종종 있었다. | ||
|
||
`Static Nested Class`, `Nested Class 종류에 따른 참조 스코프 범위`, 그리고 `익명 클래스` 등이 그러했다. | ||
|
||
결과적으로 현 챕터에 관한 내용은 그냥 알고있는게 중요한게 아니라 실제로 적용해보면서 체감하는게 중요하다. 어서 프로젝트에서 써먹어 볼 수 있으면 좋겠다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package scripts.ch_16.code; | ||
|
||
public class InputBox { | ||
public static final int KEY_DOWN = 2; | ||
public static final int KEY_UP = 4; | ||
KeyEventListener listener; | ||
|
||
public void setKeyListener(KeyEventListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
public void listenerCalled(int eventype) { | ||
if (eventype == KEY_DOWN) { | ||
listener.onKeyDown(); | ||
} | ||
else if (eventype == KEY_UP) { | ||
listener.onKeyUp(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package scripts.ch_16.code; | ||
|
||
public interface KeyEventListener { | ||
public void onKeyDown(); | ||
public void onKeyUp(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package scripts.ch_16.code; | ||
|
||
public class MyPage { | ||
|
||
public static InputBox input; | ||
|
||
static { | ||
input = new InputBox(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
MyPage testMyPage = new MyPage(); | ||
|
||
testMyPage.setUI(); | ||
testMyPage.pressKey(); | ||
} | ||
|
||
public void setUI() { | ||
KeyEventListener listener = new KeyEventListener() { | ||
@Override | ||
public void onKeyDown() { | ||
System.out.println("Key Down"); | ||
} | ||
|
||
@Override | ||
public void onKeyUp() { | ||
System.out.println("Key Up"); | ||
} | ||
}; | ||
|
||
MyPage.input.setKeyListener(listener); | ||
} | ||
|
||
public void pressKey() { | ||
MyPage.input.listenerCalled(InputBox.KEY_DOWN); | ||
MyPage.input.listenerCalled(InputBox.KEY_UP); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
### Chapter 16 : 클래스 안에 클래스가 들어갈 수도 있구나 - 정리해 봅시다. | ||
|
||
--- | ||
|
||
#### 직접 해봅시다 | ||
|
||
여러분들은 자바 기반의 `UI` 를 처리하려고 한다. `InputBox` 라는 클래스는 다음과 같이 구현되어 있다. | ||
|
||
```java | ||
package c.inner.practice; | ||
|
||
public class InputBox { | ||
public InputBox() {} | ||
|
||
KeyEventListener listener; | ||
public void setKeyListener(KeyEventListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
public static final int KEY_DOWN = 2; | ||
public static final int KEY_UP = 4; | ||
public void listenerCalled(int eventType) { | ||
if (eventType == KEY_DOWN) { | ||
listener.onKeyDown(); | ||
} | ||
else if (eventType == KEY_UP) { | ||
listener.onKeyUp(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
그리고 `setKeyListener` 에서 사용하는 `KeyEventListener` 인터페이스는 다음과 같이 정의되어 있다. | ||
|
||
```java | ||
package c.inner.practice; | ||
|
||
public interface KeyEventListener { | ||
public void onKeyDown(); | ||
public void onKeyUp(); | ||
} | ||
``` | ||
|
||
- `[1]` : `c.inner.practice` 패키지에 `MyPage` 라는 클래스를 만들고 `main()` 메서드를 추가하자. | ||
- `[2]` : `MyPage` 클래스에 `input` 이라는 이름의 `InputBox` 클래스 변수를 선언하자. | ||
- `[3]` : `MyPage` 클래스에 `setUI()` 라는 리턴값이 없는 메서드를 만들자. | ||
- `[4]` : `setUI()` 메서드에서 `input` 객체를 초기화하자. | ||
- `[5]` : `setUI()` 메서드에 `KeyEventListener` 인터페이스를 구현한 익명 클래스를 만들자. `onKeyDown()` 메서드가 호출되었을 때에는 `"Key Down"` 이, `onKeyUp()` 메서드가 호출되었을 때에는 `"Key Up"` 이 출력되도록 하자. | ||
- `[6]` : `setUI()` 메서드에서 `[5]` 에서 생성한 `listener` 를 `input` 객체의 `setKeyListener()` 메서드에 매개 변수로 넣어주자. | ||
- `[7]` : `MyPage` 클래스에 `pressKey()` 메서드를 추가하자. 이 메서드 내에서는 `InputBox` 클래스에 선언된 `listenerCalled()` 메서드를 호출하여 `onKeyDown()` 이벤트와 `onKeyUp()` 이벤트가 수행되도록 하자. | ||
- `[8]` : `MyPage` 클래스의 `main()` 메서드에서는 `setUI()` 메서드와 `pressKey()` 메서드를 차례로 호출하도록 하자. 이제 컴파일 및 실행을 해보자. 결과는 다음과 같다. | ||
|
||
``` | ||
Kye Down | ||
Key Up | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
### Chapter 16 : 클래스 안에 클래스가 들어갈 수도 있구나 - 정리해 봅시다. | ||
|
||
- [`문제 설명`](./README.md) | ||
|
||
--- | ||
|
||
문제의 설명을 따라 차근차근 진행하면 어렵지 않게 해결할 수 있다. | ||
|
||
다만 문제 `[2]` 에서 `"input 이라는 InputBox 클래스 변수 를 선언"` 하라 하였는데, 이후 문제를 보면 `클래스 변수` 가 아니라 인스턴스 변수로 선언해야 한다. | ||
|
||
나는 그냥 `클래스 변수` 로 만들었다. |
Oops, something went wrong.