CodeGym /ํ–‰๋™ /์ž๋ฐ” ์ฝ”์–ด /๊ฐœ์ฒด ๋ฐฐ์—ด ์ €์žฅ

๊ฐœ์ฒด ๋ฐฐ์—ด ์ €์žฅ

์ž๋ฐ” ์ฝ”์–ด
๋ ˆ๋ฒจ 10 , ๋ ˆ์Šจ 1
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•ˆ๋…•ํ•˜์„ธ์š”, ์•„๋ฏธ๊ณ ! ์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” ๋˜ ๋‹ค๋ฅธ ํฅ๋ฏธ๋กœ์šด ์ฃผ์ œ์— ๋Œ€ํ•ด ๋ฐฐ์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ๊ฐ์ฒด ์ €์žฅ ๋ฐ ๋กœ๋“œ (์žฌ๊ตฌ์„ฑ) . Cat ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค."

์•”ํ˜ธ
class Cat
{
 public String name;
 public int age;
 public int weight;
}

๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š” ํŽธ๋ฆฌํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•”ํ˜ธ
class Cat {
    public String name;
    public int age;
    public int weight;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.println(weight);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
        weight = Integer.parseInt(reader.readLine());
    }
}

"์™€์šฐ! ์ •๋ง ์‰ฝ๋„ค์š”! ๊ฐ ์ธ์ˆ˜์˜ ๊ฐ’์„ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์“ฐ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ๋กœ๋“œํ•  ๋•Œ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ์ฝ์Šต๋‹ˆ๋‹ค. ์™„๋ฒฝํ•œ ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค."

"๊ณ ๋ง™์Šต๋‹ˆ๋‹ค, Amigo. ์ด์ œ ์ด ํด๋ž˜์Šค ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์ €์žฅ ๋ฐ ๋กœ๋“œ ๋ฐฉ๋ฒ•์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

์•”ํ˜ธ
class Cat
{
 public String name;
 public int age;
 public int weight;
}
class Dog
{
 public String name;
 public int age;
}
class Human
{
 public Cat cat;
 public Dog dog;
}

๊ฐœ ํ•œ ๋งˆ๋ฆฌ์™€ ๊ณ ์–‘์ด ํ•œ ๋งˆ๋ฆฌ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” Human ๊ฐœ์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

"ํ•ด๊ฒฐ์ฑ…์ด ์žˆ์Šต๋‹ˆ๋‹ค."

์•”ํ˜ธ
class Cat {
    public String name;
    public int age;
    public int weight;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.println(weight);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
        weight = Integer.parseInt(reader.readLine());
    }
}
์•”ํ˜ธ
class Dog {
    public String name;
    public int age;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
    }
}
์•”ํ˜ธ
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        cat.save(writer);
        dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {
        cat.load(reader);
        dog.load(reader);
    }
}

"์•„์ฃผ ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์ด๊ตฐ์š”. ๊ทธ๋Ÿฐ๋ฐ ์ธ๊ฐ„์—๊ฒŒ ๊ฐœ๋Š” ์žˆ๋Š”๋ฐ ๊ณ ์–‘์ด๊ฐ€ ์—†๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?"

null ๊ฒ€์‚ฌ๋Š” ์–ด๋””์— ์žˆ์Šต๋‹ˆ๊นŒ?

"์ง€๊ธˆ ํ•ด๊ฒฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

์•”ํ˜ธ
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        if (cat != null)
            cat.save(writer);
        if (dog != null)
            dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {
        cat = new Cat();
        cat.load(reader);
        dog = new Dog();
        dog.load(reader);
    }
}

"์—ฌ์ „ํžˆ ์ •ํ™•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."

1) ์‚ฌ๋žŒ์—๊ฒŒ ๊ณ ์–‘์ด๋‚˜ ๊ฐœ๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ load ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๊ณ ์–‘์ด๋‚˜ ๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

2) ๊ฐœ๋งŒ ์ €์žฅํ•˜๋ฉด ๋กœ๋“œ๋  ๋•Œ ๊ณ ์–‘์ด๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.

"๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?"

" ๋ณ€์ˆ˜ ์“ฐ๊ธฐ๋ฅผ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ฝ๋Š” ๋™์•ˆ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค . ์ €์žฅ ์ž‘์—… ์ค‘์— null์ธ ๋ณ€์ˆ˜๊ฐ€ ๋กœ๋“œ ์ž‘์—… ์ค‘์— null๋กœ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด ๋ฒ„์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

์•”ํ˜ธ
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        String isCatPresent = cat != null ? "yes" : "no";
        writer.println(isCatPresent);
        writer.flush();

        if (cat != null)
            cat.save(writer);

        String isDogPresent = dog != null ? "yes" : "no";
        writer.println(isDogPresent);
        writer.flush();

        if (dog != null)
            dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {

        String isCatPresent = reader.readLine();
        if (isCatPresent.equals("yes")) {
            cat = new Cat();
            cat.load(reader);
        }

        String isDogPresent = reader.readLine();
        if (isDogPresent.equals("yes")) {
            dog = new Dog();
            dog.load(reader);
        }
    }
}

"์˜ˆ, ์ด ์†”๋ฃจ์…˜์ด ๋งˆ์Œ์— ๋“ญ๋‹ˆ๋‹ค."

"๋„ค, ์ข‹๋„ค์š”."

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION