DevLog ๐Ÿ˜ถ

[์ดํŒฉํ‹ฐ๋ธŒ์ž๋ฐ”] ์•„์ดํ…œ 03. private ์ƒ์„ฑ์ž๋‚˜ ์—ด๊ฑฐ ํƒ€์ž…์œผ๋กœ ์‹ฑ๊ธ€ํ„ด์ž„์„ ๋ณด์ฆํ•˜๋ผ ๋ณธ๋ฌธ

๐Ÿ“–/Effective Java

[์ดํŒฉํ‹ฐ๋ธŒ์ž๋ฐ”] ์•„์ดํ…œ 03. private ์ƒ์„ฑ์ž๋‚˜ ์—ด๊ฑฐ ํƒ€์ž…์œผ๋กœ ์‹ฑ๊ธ€ํ„ด์ž„์„ ๋ณด์ฆํ•˜๋ผ

dolmeng2 2023. 4. 4. 23:29

๐Ÿ’ฌ ์ดํŒฉํ‹ฐ๋ธŒ์ž๋ฐ” ์•„์ดํ…œ 3์„ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.


 

โœ”๏ธ ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค

์ธ์Šคํ„ด์Šค๋ฅผ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค. ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋งŒ, ๊ฒฐํ•ฉ๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉฐ ํด๋ผ์ด์–ธํŠธ์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ์กด์žฌํ•œ๋‹ค.

-> ์ธ์Šคํ„ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๋งŒ๋“  ์‹ฑ๊ธ€ํ„ด์ด ์•„๋‹ˆ๋ผ๋ฉด, ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค๋ฅผ mock ๊ฐ์ฒด๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

๐Ÿ’ฌ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ์ƒ์„ฑ์ž๋ฅผ ๋ชจ๋‘ private๋กœ ๋งŒ๋“ค๊ณ , ์ธ์Šคํ„ด์Šค์˜ ์œ ์ผํ•œ ์ ‘๊ทผ์ž๋ฅผ public static์œผ๋กœ ์ƒ์„ฑํ•˜๊ธฐ

public class Crew {
    public static final Crew INSTANCE = new Crew();
    private Crew() {
		...
    }
    public void hello() {...}
}

์ด๋ ‡๊ฒŒ ๋˜๋ฉด, Crew ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ static final ํ•„๋“œ๋กœ ์ธํ•ด์„œ private ์ƒ์„ฑ์ž๊ฐ€ ๋”ฑ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.

= ์ „์ฒด ์‹œ์Šคํ…œ์—์„œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋Š” ๋”ฑ 1๊ฐœ์ž„์„ ๋ณด์ฆํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

 

2. ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

public class Crew {
    private static final Crew INSTANCE = new Crew();
    private Crew() {
		...
    }
    public static Crew getInstance() {
        return INSTANCE;
    }
    public void hello() {...}
}

์ด์ „์— ์ƒ์„ฑํ•œ ํ•„๋“œ๋ฅผ private๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ, ํ•ด๋‹น ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

 

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ ์žฅ๋‹จ์ ์„ ๋น„๊ตํ•ด๋ณด์ž.

1๋ฒˆ → ํด๋ž˜์Šค๊ฐ€ ์‹ฑ๊ธ€ํ„ด์ž„์„ API์— ๋ช…๋ฐฑํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚ธ๋‹ค. ๊ฐ„๊ฒฐํ•˜๋‹ค.

2๋ฒˆ → API์˜ ๋ณ€๊ฒฝ์—†์ด ์‹ฑ๊ธ€ํ„ด์ด ์•„๋‹ˆ๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ์Šค๋ ˆ๋“œ๋ณ„๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค๋“ ์ง€…?), ์ œ๋„ค๋ฆญ ์‹ฑ๊ธ€ํ„ด ํŒฉํ„ฐ๋ฆฌ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜, ์ •์  ํŽ™ํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ ์ฐธ์กฐ๋ฅผ ๊ณต๊ธ‰์ž๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (ex. Supplier)

 

๐Ÿ’ก ์ œ๋„ค๋ฆญ ์‹ฑ๊ธ€ํ„ด ํŒฉํ„ฐ๋ฆฌ๋Š” ์ถ”ํ›„ ์†Œ๊ฐœ๋˜๊ฒ ์ง€๋งŒ, ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ์š”์ฒญ์œผ๋กœ ๋“ค์–ด์˜จ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋งž๊ฒŒ, ๊ทธ ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ์ •์  ํŒฉํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

// ImmutableCollections.java
static <E> Set<E> emptySet() {
        return (Set<E>) SetN.EMPTY_SET;
}

๊ทธ๋ƒฅ ์ œ๋„ค๋ฆญ + ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•˜๋‹ค :D

 

 

๐Ÿ’ฌ ์ง๋ ฌํ™” ๊ด€์ 

์‹ฑ๊ธ€ํ„ด ํด๋ž˜์Šค๋ฅผ ์ง๋ ฌํ™”ํ•˜๋Š” ๊ฒƒ์€ ์ฃผ์˜๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ํ„ด ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ์ง๋ ฌํ™”ํ•œ ๋’ค, ๋‹ค์‹œ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ†ตํ•ด ๋ณต์›ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๊ฐ€ ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด, ์‹ฑ๊ธ€ํ„ด ํด๋ž˜์Šค๋Š” 'ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ƒ์„ฑํ•œ๋‹ค'๋ผ๋Š” ๊ฐœ๋…๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋ž˜์„œ, ์‹ฑ๊ธ€ํ„ด ํด๋ž˜์Šค์˜ ์ง๋ ฌํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ, ์ธ์Šคํ„ด์Šค ํ•„๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋ฅผ transient๋กœ ์„ค์ •ํ•˜์—ฌ ์ง๋ ฌํ™”์—์„œ ์ œ์™ธ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.

public class Singleton implements Serializable {
    private static final long serialVersionUID = 1L;
    private transient String name;

    private static final Singleton instance = new Singleton("Test");

    private Singleton(final String name) {
        this.name = name;
    }

    public static Singleton getInstance() {
        return instance;
    }

    @Override
    public String toString() {
        return "Singleton{" +
                "name='" + name + '\'' +
                '}';
    }
}

transient ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํ•„๋“œ๊ฐ’ ์ž์ฒด๋Š” ์œ ์ง€๋˜์ง€๋งŒ, ๋‚ด๋ถ€์ ์œผ๋กœ null ๊ฐ’์ด ๋Œ€์ž…๋œ๋‹ค.

์ฆ‰, ์ƒ์„ฑํ•œ instance์— ๋Œ€ํ•ด ์—ญ์ง๋ ฌํ™” ์‹œ ๊ฒฐ๊ณผ๊ฐ’์ด null์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

๋งŒ์•ฝ, transient๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ง๋ ฌํ™”๋ฅผ ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

public class Main {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();

        // ์ง๋ ฌํ™”๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
        try (FileOutputStream fileOut = new FileOutputStream("singleton.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
            out.writeObject(singleton);
        } catch (IOException i) {
            i.printStackTrace();
        }

        // ์—ญ์ง๋ ฌํ™”๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
        Singleton deserializedSingleton;
        try (FileInputStream fileIn = new FileInputStream("singleton.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn)) {
            deserializedSingleton = (Singleton) in.readObject();
        } catch (IOException | ClassNotFoundException i) {
            i.printStackTrace();
            return;
        }

        // ์ถœ๋ ฅ
        System.out.println(deserializedSingleton.toString());
    }
}

transient๋ฅผ ๋ถ™์ด์ง€ ์•Š์•˜์„ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด null์ด ์•„๋‹Œ ์‹ค์ œ ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค.

๋ฐ˜๋ฉด์—, transient๋ฅผ ๋ถ™์ด๋ฉด ์œ„์™€ ๊ฐ™์ด null์ด ๋“ค์–ด๊ฐ„๋‹ค.

 

๋‹ค๋งŒ, transient ํ‚ค์›Œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. 

์œ„ ์ฝ”๋“œ์—์„œ instance์˜ ๊ฒฝ์šฐ static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ํด๋ž˜์Šค ์ž์ฒด์— ์†ํ•˜๊ฒŒ ๋œ๋‹ค.

์ฆ‰, ํด๋ž˜์Šค ์ž์ฒด๊ฐ€ ์ง๋ ฌํ™”๋˜๋ฉด์„œ ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ getInstance๊ฐ€ ์•„๋‹Œ ์ €์žฅ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ณต์›๋˜๋ฉฐ, ๊ธฐ์กด์˜ ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค์™€ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ์ถ”๊ฐ€์ ์œผ๋กœ readResolve()๋ฅผ ์žฌ์ •์˜ ํ•ด์•ผ ํ•œ๋‹ค.

 

boolean result = singleton == deserializedSingleton;
System.out.println("singleton == deserializedSingleton : " + result);

์‹ค์ œ๋กœ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด, false๊ฐ€ ๋‚˜์˜จ๋‹ค.

public class Singleton implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }

    protected Object readResolve() {
        return instance;
    }
}

๋ฐ˜๋ฉด์— readResolve() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค ๋Œ€์‹ ์— ๊ธฐ์กด์— ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก

๋งŒ๋“ค์–ด์„œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ด๋ ‡๊ฒŒ true๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

 

๐Ÿ’ฌ ์™œ readResolve()๋ฅผ ์žฌ์ •์˜ํ•˜๋ฉด ๋˜๋Š” ๊ฑธ๊นŒ?

๊ถ๊ธˆํ•ด์„œ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€๋ฅผ ์กฐ๊ธˆ ๊นŒ๋ดค๋‹ค.

try (FileInputStream fileIn = new FileInputStream("singleton.ser");
     ObjectInputStream in = new ObjectInputStream(fileIn)) {
     deserializedSingleton = (Singleton) in.readObject(); // Here!
	...

์œ„ ์ฝ”๋“œ์—์„œ readObject()์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์‚ดํŽด๋ณด์ž.

์ด๋ ‡๊ฒŒ bypass ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š”๋ฐ, ๋˜ ๋”ฐ๋ผ๊ฐ€๋ณด์ž.

์ด ๋ฉ”์„œ๋“œ์—์„œ ์ค‘๊ฐ„์— ์žˆ๋Š” readObject0 ๋ฉ”์„œ๋“œ๋ฅผ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€๋ณด์ž!

๊ทธ๋Ÿผ ์—ฌ๊ธฐ์„œ object case์— ๋Œ€ํ•ด readOrdinaryObject()๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ!

 

๋“ค์–ด๊ฐ€ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด readResolve() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ณ , ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์— invoke๋ฅผ ํ†ตํ•ด์„œ ํ˜ธ์ถœํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.์‹ค์ œ๋กœ invokeReadResolve() ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. (์ด๋ž˜์„œ ๋ฉ”์„œ๋“œ์˜ ๋„ค์ด๋ฐ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ๊นจ๋‹ซ๋Š”๋‹ค ๐Ÿ‘)

์š”๋Ÿฐ ์‹์œผ๋กœ ๋‚ด๋ถ€์—์„œ invoke๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค! ใ…Žใ…Ž

 

 

์ถ”๊ฐ€์ ์œผ๋กœ ์ฑ…์—์„œ๋Š” ์‹ฑ๊ธ€ํ„ด์˜ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์œผ๋กœ enum์„ ์†Œ๊ฐœํ•œ๋‹ค.

enum์˜ ๊ฒฝ์šฐ JVM์ด ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

public enum Crew {
    INSTANCE;

    public void hello() {...}
}

๋˜ํ•œ, enum์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ Serializableํ•˜๋‹ค. enum์˜ ๊ฒฝ์šฐ ์ง๋ ฌํ™”๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ObjectOutputStream์—์„œ name()์˜ return ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์—ญ์ง๋ ฌํ™” ์‹œ์—๋Š” valueOf๋ฅผ ํ†ตํ•ด enum์˜ constant ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค๊ณ  ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, readResolve ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ณ„๋„๋กœ ์ปค์Šคํ…€์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, serialVersionUID์˜ ๊ฒฝ์šฐ 0L๋กœ ๊ณ ์ •์ด ๋˜์–ด์žˆ๋‹ค.

 

์ฝ”๋“œ๋กœ ํ™•์ธํ•ด๋ณด์ž!

case๊ฐ€ ์นœ์ ˆํ•˜๊ฒŒ enum์— ๋Œ€ํ•ด์„œ ๋‚˜๋ˆ„์–ด์ ธ์žˆ๋‹ค ใ…Žใ…Ž (์—ญ์ง๋ ฌํ™” ์‹œ ์‚ฌ์šฉํ•˜๋Š” readObject() ๋ฉ”์„œ๋“œ๋ฅผ ์•„๊นŒ์ฒ˜๋Ÿผ ๊นŒ๋ณธ ๊ฒƒ์ด๋‹ค.)

์—ฌ๊ธฐ์„œ Enum.valueOf๋ฅผ ํ†ตํ•ด์„œ result๋ฅผ ๋‹ด์•„์ฃผ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

์ง๋ ฌํ™” ๊ณผ์ •๋„ ์‚ดํŽด๋ณด์ž.

writeObject์˜ ๋ฉ”์„œ๋“œ ์ค‘์—์„œ writeObject0๋ฅผ ๋“ค์–ด๊ฐ€๋ณด์ž!

๊ทธ๋Ÿผ Enum ํƒ€์ž…์— ๋Œ€ํ•ด์„œ writeEnum์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉฐ,

๋‚ด๋ถ€์ ์œผ๋กœ en.name()์„ ํ†ตํ•ด์„œ writeString์„ ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

์ฑ…์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ์ด๊ฒŒ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ ๋งŒ๋“ค๋ ค๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์†์„ ํ•ด์•ผ ๋œ๋‹ค๋ฉด ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. (enum ์ž์ฒด๋Š” ์ถ”๊ฐ€ ์ƒ์†์ด ์•ˆ ๋˜๋‹ˆ๊นŒ)

 


์ฑ… ๋‚ด์šฉ์€ ๊ฐ„๊ฒฐํ•œ๋ฐ ์ง๋ ฌํ™” ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ์ •๋ฆฌํ•˜๋Š” ๋ฐ ๋” ๊ฑธ๋ ธ๋‹ค ๐Ÿ’ฆ

๊ทธ๋ž˜๋„ ์ฝ”๋“œ ๊นŒ๋ณด๋Š” ๊ฑด ํ•ญ์ƒ ์žฌ๋ฐŒ๋‹ค ใ…Žใ…Ž ์ดํŒฉํ‹ฐ๋ธŒ ๋‚จ์€ ๊ฒƒ๋„ ์–ผ๋ฅธ ์ •๋ฆฌํ•ด์•ผ์ง€...

Comments