-
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.
Object를 활용한 예시들..
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
--- | ||
title: Object | ||
date: 2024-06-21 | ||
description: Object 활용 | ||
--- | ||
--- | ||
|
||
## 1 . instanceof | ||
--- | ||
|
||
```java | ||
private static void method(Object obj) { | ||
if (obj instanceof InstanceType instance) { | ||
instance.method(); | ||
} | ||
} | ||
``` | ||
|
||
- 여기서 InstanceType은 검사하려는 특정 타입을, instance는 그 타입으로 다운캐스팅 된 객체를 참조하는 변수를 나타내고, 이 패턴은 특히 타입이 여러 가지일 수 있는 매개변수를 처리할 때 많이 사용. | ||
- 타입 검사와 다운캐스팅을 한 줄의 코드로 수행할 수 있음. |