-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
6μ₯/41_μ μνλ €λ_κ²μ΄_νμ
μ΄λΌλ©΄_λ§μ»€_μΈν°νμ΄μ€λ₯Ό_μ¬μ©νλΌ_κΉλ―Όκ±Έ.md
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,102 @@ | ||
# μ μνλ €λ κ²μ΄ νμ μ΄λΌλ©΄ λ§μ»€ μΈν°νμ΄μ€λ₯Ό μ¬μ©νλΌ. | ||
|
||
## λ§μ»€ μΈν°νμ΄μ€ | ||
- μμ μ ꡬννλ ν΄λμ€κ° νΉμ μμ±μ κ°μ§μ λνλ΄λ μΈν°νμ΄μ€ | ||
- μΆμ λ©μλλ , νλλ , default, static λ©μλλ μ무κ²λ μλ€. | ||
- Cloneable, Serializable κ°μ μΈν°νμ΄μ€ | ||
|
||
## λ§μ»€ μ΄λ Έν μ΄μ | ||
- ν΄λΉ μμκ° νΉμ μμ±μ κ°μ§μ λνλ΄λ μλν μ΄μ | ||
- @Override, @FunctionalInterface, @SafeVarargs, @Native | ||
|
||
|
||
## λ§μ»€ μλν μ΄μ vs λ§μ»€ μΈν°νμ΄μ€ | ||
|
||
### 1. λ§μ»€ μΈν°νμ΄μ€λ [νμ ] μΌλ‘ μΈ μ μλ€ ! | ||
- λ§μ»€ μΈν°νμ΄μ€μ λ§μ»€ μλν μ΄μ λͺ¨λ ν΄λμ€κ° μ΄λ€ μμ±μ κ°μ§λ€λ νμλ₯Ό ν μ μλ€. | ||
- λ§μ»€ μΈν°νμ΄μ€λ νμ μΌλ‘ μ¬μ©νμ¬ **μ»΄νμΌνμ** μ μ€λ₯λ₯Ό κ²μΆν μ μλ€. | ||
- λ§μ»€ μλν μ΄μ μ **λ°νμ**μμμΌ μ€λ₯λ₯Ό κ²μΆν μ μλ€. | ||
|
||
<br> | ||
|
||
[κ°μ²΄λ₯Ό νμΌμ μμ±νλ λ©μλ] | ||
```java | ||
// 맀κ°λ³μλ‘ Serializeable νμ μ λ°λλ‘ νλ€. | ||
public static void newWriteObject(Serializable object, String path) { | ||
File file = new File(path); | ||
|
||
try (ObjectOutputStream oops = new ObjectOutputStream(new FileOutputStream(file))) { | ||
oops.writeObject(object); | ||
} catch (IOException e) { | ||
System.out.println("λ°νμμ μλ¬ λ°μ"); | ||
} | ||
} | ||
``` | ||
[λ©μλ μ¬μ©] | ||
|
||
![μ΄λ―Έμ§](https://github.com/cmg1411/effectiveJava/blob/master/src/main/java/Chapter6/Day41/%EC%97%90%EB%9F%AC%EC%97%90%EB%9F%AC.png) | ||
|
||
- μΈμλ‘ λκΈ°λ €λ MeetCoder κ°μ²΄λ Serializable μ ꡬννμ§ μμλ€ ! | ||
- κ²°κ³Όλ‘ μλμ κ°μ΄ μ»΄νμΌμ€λ₯κ° λ리λ₯Ό μΉλ€. | ||
- ν΄κ²°λ²μΌλ‘λ MeetCoder ν΄λμ€μ Serializable μ implements νμ¬ νμνμ μΌλ‘ λ§λ€κΈ°λ§ νλ©΄ λλ€. | ||
|
||
<br> | ||
|
||
[λ§μ»€ μλν μ΄μ ] | ||
- λ°λ©΄ μ΄λ₯Ό λ§μ»€ μλν μ΄μ μΌλ‘ ν΄κ²°νλ €λ©΄, μλν μ΄μ νλ‘μΈμλ₯Ό μ΄μ©νμ¬ instanceof λ₯Ό μ΄μ©νμ¬ λ°νμμμΌ μλ¬λ₯Ό κ²μΆν μ μλ€. | ||
|
||
<br> | ||
<br> | ||
|
||
## Serializable μ writeObject() | ||
|
||
writeObject λ©μλμμλ ```writeObject0(obj, false);``` λ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλ€. | ||
```java | ||
private void writeObject0(Object obj, boolean unshared) | ||
throws IOException { | ||
... | ||
} else if (obj instanceof Serializable) { | ||
writeOrdinaryObject(obj, desc, unshared); | ||
} else { | ||
if (extendedDebugInfo) { | ||
throw new NotSerializableException( | ||
cl.getName() + "\n" + debugInfoStack.toString()); | ||
} else { | ||
throw new NotSerializableException(cl.getName()); | ||
} | ||
} | ||
... | ||
``` | ||
- Object λ‘ κ°μ²΄λ₯Ό λ°μ ν | ||
- μ¬λ¬ 쑰건μ κ²μ¬νκ³ λ§μ§λ§μΌλ‘ Serializable μ ꡬννλμ§ instanceof λ‘ νμ κ²μ¬λ₯Ό νκ³ μλ€. | ||
- λ§μ»€ μΈν°νμ΄μ€κ° νμ μΌλ‘ μΈ μ μλ€λ μ₯μ μ μ΄λ¦¬μ§ λͺ»νκ³ μλ€. | ||
|
||
|
||
<br> | ||
<br> | ||
|
||
### 2. λ§μ»€ μΈν°νμ΄μ€λ μ μ© λμμ λ μ λ°νκ² μ§μ ν μ μλ€. | ||
- λ§μ»€ μλν μ΄μ μ @Retention(RetentionPolicy.TYPE) μλν μ΄μ μ ν΅ν΄ ν΄λμ€, μΈν°νμ΄μ€, enum, μλν μ΄μ μλ§ λ¬ μ μλ λ§μ»€λ₯Ό λ§λ€ μ μλ€. | ||
- νμ§λ§ λ§μ»€ μΈν°νμ΄μ€λ μ΄λ³΄λ€ λ μ λ°ν μ νμ λ μ μλ€. | ||
|
||
μλ₯Ό λ€μ΄, Person μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€μλ§ MeetCoder μ€ν°λμμ΄λΌλ λ§μ»€λ₯Ό λ¬κ³ μΆλ€λ μꡬμ¬νμ΄ μλ€κ³ μκ°ν΄λ³΄μ. | ||
κ·Έλ¬λ©΄ MeetCoder μΈν°νμ΄μ€ Person λ‘ νμ₯νκ³ , MeetCoder μΈν°νμ΄μ€λ₯Ό ꡬννλ©΄ λλ€. | ||
MeetCoderλ₯Ό ꡬνν ν΄λμ€λ μλμΌλ‘ Person μΈν°νμ΄μ€μ νμ νμ μ΄ λλ κ²μ΄κ³ , MeetCoder κ° λ§μ»€ μΈν°νμ΄μ€μΈ κ²μ΄λ€. | ||
|
||
μ± μ μμμμλ Collection μ ꡬνν ν΄λμ€μμλ§ Set μ΄λΌλ λ§μ»€ μΈν°νμ΄μ€λ₯Ό μ¬μ©ν μ μλ€κ³ νλ€. | ||
|
||
<br> | ||
<br> | ||
|
||
### 3. μλν μ΄μ μ μΈμ μΈκΉ ? | ||
- λ§νΉνλ €λ κ³³μ΄ ν΄λμ€, μΈν°νμ΄μ€κ° μλ λ. | ||
- μλν μ΄μ μ μ κ·Ή νμ©νλ νλ μμν¬μ μΌλΆμ λ§μ»€λ₯Ό λ¬ λ. | ||
|
||
<br> | ||
<br> | ||
|
||
#### κ²°λ‘ | ||
- λ§νΉλ κ°μ²΄λ₯Ό 맀κ°λ³μλ‘ λ°λ λ©μλλ₯Ό μμ±ν μΌμ΄ μμκΉ ? -> [μ] λΌλ©΄ λ§μ»€ μΈν°νμ΄μ€λ₯Ό μ°μ ! | ||
- Retention μ΄ ElementType.TYPE μΈ λ§μ»€ μλν μ΄μ μ μ°κ³ μλ€ ? -> λ§μ»€ μΈν°νμ΄μ€λ‘ λ°κΏμ§ κ³ λ―Όν΄λ³΄μ. | ||
|
||
[java.util.Collection] |