"์๋ ํ์ธ์, ์๋ฏธ๊ณ ! ์ค๋ ์ฐ๋ฆฌ๋ ๋ ๋ค๋ฅธ ํฅ๋ฏธ๋ก์ด ์ฃผ์ ์ ๋ํด ๋ฐฐ์ธ ๊ฒ์ ๋๋ค. ํนํ ๊ฐ์ฒด ์ ์ฅ ๋ฐ ๋ก๋ (์ฌ๊ตฌ์ฑ) . 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);
}
}
}
"์, ์ด ์๋ฃจ์ ์ด ๋ง์์ ๋ญ๋๋ค."
"๋ค, ์ข๋ค์."
GO TO FULL VERSION