File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed
Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -203,14 +203,14 @@ public class Animal {
203203}
204204```
205205``` JAVA
206+ // 예시 1
206207public static void main(String [] args) {
207208 // 스태틱 메소드 디스패치
208209 Animal animal = new Animal ();
209210 System . out. println(animal. method());
210211}
211- ```
212- 또는
213- ``` JAVA
212+
213+ // 예시 2
214214public class Example {
215215 private Animal animal;
216216
@@ -233,28 +233,32 @@ public class Example {
233233public interface Animal {
234234 String method ();
235235}
236+
236237public class Dog implements Animal {
238+
237239 @Override
238240 public String method () { . . . }
239241
240242 public void bark () { . . . }
241243}
244+
242245public class Cat implements Animal {
246+
243247 @Override
244248 public String method () { . . . }
245249
246250 public void meow () { . . . }
247251}
248252```
249253``` JAVA
254+ // 예시 1
250255public static void main(String [] args) {
251256 // 다이나믹 메소드 디스패치
252257 Animal animal = new Dog ();
253258 System . out. println(animal. method());
254259}
255- ```
256- 또는
257- ``` JAVA
260+
261+ // 예시 2
258262public class Example {
259263 private Animal animal;
260264
@@ -270,6 +274,7 @@ public class Example {
270274```
271275 - 런타임 전에는 객체 생성이 되지 않기 때문에 ``` Animal animal = new Dog(); ``` 를 해도, 컴파일러는 ``` Dog ``` 가 생성됨을 알 수 없으므로 ``` Animal ``` 이 정의한 ``` method() ``` 메소드만 접근 가능
272276 - ``` Example ``` 클래스의 생성자 인자로 넘어오는 매개값의 타입이 ``` Dog ``` 인지 ``` Cat ``` 인지(혹은 그 외)는 실행 시 확인 가능
277+ - 실행 시 동적으로 확인해서 다이나믹 디스패치
273278
274279자주 사용하는 컬렉션 타입에도 다이나믹 메소드 디스패치 사용
275280``` JAVA
You can’t perform that action at this time.
0 commit comments