"์๋ , ์๋ฏธ๊ณ !"
"์๋ , ์๋ฆฌ!"
"์ค๋์ ๋ฆฌ์์ ์ ๊ฐ ์ ๋ค๋ฆญ์ ๋ํ ๋ชจ๋ ๊ฒ์ ์๋ ค๋๋ฆฌ๊ฒ ์ต๋๋ค."
"์ ๊น, ๋๋ ์ด๋ฏธ ๊ฑฐ์ ๋ชจ๋ ๊ฒ์ ์๊ณ ์๋ ๊ฒ ๊ฐ์."
"๊ฑฐ์ ์ ๋ถ์ง๋ง ์ ๋ถ๋ ์๋๋๋ค."
"์ ๋ง์? ์๊ฒ ์ต๋๋ค. ๋ค์ ์ค๋น๊ฐ ๋์์ต๋๋ค."
"๊ทธ๋ผ ์์ํ๊ฒ ์ต๋๋ค."
"Java์์ ์ ๋ค๋ฆญ์ ์ ํ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ํด๋์ค์ ๋๋ค."
"์ ๋ค๋ฆญ์ด ๋ฐ๋ช ๋ ์ด์ ๋ ์ฝ๋์ ์ฃผ์์ ์ฐธ์กฐํ์ญ์์ค."
ArrayList stringList = new ArrayList();
stringList.add("abc"); // Add a string to the list
stringList.add("abc"); // Add a string to the list
stringList.add( 1 ); // Add a number to the list
for(Object o: stringList)
{
String s = (String) o; // There will be an exception here when we get to the integer
}
์ ๋ค๋ฆญ์ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ:
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("abc"); // Add a string to the list
stringList.add("abc"); // Add a string to the list
stringList.add( 1 ); // There will be a compilation error here
for(Object o: stringList)
{
String s = (String) o;
}
"์ด ์ฝ๋๋ ์ปดํ์ผ๋์ง ์์ผ๋ฉฐ ์๋ชป๋ ๋ฐ์ดํฐ ์ ํ์ ์ถ๊ฐํ์ฌ ๋ฐ์ํ๋ ์ค๋ฅ๋ ์ปดํ์ผ ์ค์ ๋ฐ๊ฒฌ๋ฉ๋๋ค."
"์, ์ด๋ฏธ ์๊ณ ์์ต๋๋ค."
"์ข์, ์ข์. ๋ฐ๋ณต์ ๋ฐฐ์์ ์ด๋จธ๋๋ค."
"ํ์ง๋ง Java์ ์ ์์๋ ์ ๋ค๋ฆญ์ ๋ง๋ค ๋ ์ฝ๊ฐ ๊ฒ์๋ ์ต๋๋ค. ๋งค๊ฐ ๋ณ์๋ก ๋ณธ๊ฒฉ์ ์ธ ์ ํ์ ๋ง๋๋ ๋์ ๋งค๋๋ฌ์ด ์ต์ ํ์ ๋น ์ก์ต๋๋ค. ์ค์ ๋ก๋ ์ ๋ค๋ฆญ์ ์ ํ ๋งค๊ฐ ๋ณ์์ ๋ํ ์ ๋ณด๋ฅผ ์ถ๊ฐํ์ง ์์์ต๋๋ค. ๋์ ๋ชจ๋ ์ปดํ์ผํ๋ ๋์ ๋ง๋ฒ์ด ๋ฐ์ํฉ๋๋ค."
List<String> strings = new ArrayList<String>();
strings.add("abc");
strings.add("abc");
strings.add( 1); // Compilation error
for(String s: strings)
{
System.out.println(s);
}
List strings = new ArrayList();
strings.add((String)"abc");
strings.add((String)"abc");
strings.add((String) 1); // Compilation error
for(String s: strings)
{
System.out.println(s);
}
"๊น๋ํ๋ค."
"์, ํ์ง๋ง ์ด ์ ๊ทผ ๋ฐฉ์์๋ ๋ถ์์ฉ์ด ์์ต๋๋ค. ์ ํ ๋งค๊ฐ๋ณ์์ ๋ํ ์ ๋ณด๋ ์ผ๋ฐ ํด๋์ค ๋ด์ ์ ์ฅ๋์ง ์์ต๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ๋์ค์ ์ ํ ์ญ์ ๋ก ์๋ ค์ง๊ฒ ๋์์ต๋๋ค ."
"์ฆ, ์ ํ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ณ ์ ํ ํด๋์ค๊ฐ ์๋ ๊ฒฝ์ฐ ํด๋์ค ๋ด์์ ํด๋น ํด๋์ค์ ๋ํ ์ ๋ณด๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค."
class Zoo<T>
{
ArrayList<T> pets = new ArrayList<T>();
public T createAnimal()
{
T animal = new T();
pets.add(animal)
return animal;
}
}
class Zoo
{
ArrayList pets = new ArrayList();
public Object createAnimal()
{
Object animal = new ???();
pets.add(animal)
return animal;
}
}
"์ปดํ์ผํ๋ ๋์ ๋ชจ๋ ๋งค๊ฐ๋ณ์ ์ ํ์ด Object๋ก ๋์ฒด๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋์ค ๋ด๋ถ์๋ ์ ๋ฌ๋ ์ ํ์ ๋ํ ์ ๋ณด๊ฐ ์์ต๋๋ค."
"์, ๋์ํฉ๋๋ค. ๊ทธ๊ฒ ์ต๊ณ ๊ฐ ์๋๋๋ค."
"๊ทธ๋ ๊ฒ ๋ฌด์ญ์ง ์์. ๋์ค์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์๋ ค์ค๊ฒ."
ํ์ง๋ง ๋ ์์ต๋๋ค. Java๋ฅผ ์ฌ์ฉํ๋ฉด ์ ํ ๋งค๊ฐ๋ณ์์ ์์ ์ ํ์ ์ง์ ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ์ํด extends ํค์๋๊ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด:
class Zoo<T extends Cat>
{
T cat;
T getCat()
{
return cat;
}
void setCat (T cat)
{
this.cat = cat;
}
String getCatName()
{
return this.cat.getName();
}
}
class Zoo
{
Cat cat;
Cat getCat()
{
return cat;
}
void setCat(Cat cat)
{
this.cat = cat;
}
String getCatName()
{
return this.cat.getName();
}
}
"๋ ๊ฐ์ง ์ฌ์ค์ ์ ์ํ์ญ์์ค."
"์ฒซ์งธ, ์ด๋ค ์ ํ๋ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์ ์์ต๋๋ค. Cat ๋๋ Cat์ ์์ํ๋ ํด๋์ค๋ง ์ ๋ฌํ ์ ์์ต๋๋ค."
"๋ ๋ฒ์งธ๋ก, Zoo ํด๋์ค ๋ด์์ T ์ ํ์ ๋ณ์๋ ์ด์ Cat ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์ค๋ฅธ์ชฝ ์ด์ ๊ทธ ์ด์ ๊ฐ ์ค๋ช ๋์ด ์์ต๋๋ค(T๊ฐ ์๋ ๋ชจ๋ ๊ณณ์์ Cat์ด ๋์ฒด๋๊ธฐ ๋๋ฌธ์ ๋๋ค)."
"์. Cat ๋๋ Cat์ ํ์ ํด๋์ค๊ฐ ์ ํ ์ธ์๋ก ์ ๋ฌ๋๋ค๊ณ ํ๋ฉด ์ ํ T๊ฐ ํญ์ Cat ํด๋์ค์ ๋ฉ์๋๋ฅผ ๊ฐ์ง ๊ฒ์ด๋ผ๊ณ ํ์ ํฉ๋๋ค."
"๊ธ์์, ๋๋ํ๊ตฐ์."