generics
์ง๋ค๋ฆญ์ค๋ JDK1.5์์ ์ฒ์ ๋์ ๋์๋ค. ์ด์ ์ง๋ค๋ฆญ์ค๋ฅผ ๋ชจ๋ฅด๊ณ ๋ JAVA API๋ฌธ์๋ฅผ ์ ๋๋ก ๋ณด๊ธฐ ์ด๋ ค์ธ๋งํผ ์ค์ํ์์น๋ฅผ ์ฐจ์งํ์๋ค.
์ง๋ค๋ฆญ์ค๋?
๋ฉ์๋๋ ์ปฌ๋ ์ ํด๋์ค์ ์ปดํ์ผ์์ ํ์ ์ฒดํฌ๋ฅผ ํด์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
๊ฐ์ฒด์ ํ์ ์์ ์ฑ์ ๋์ด๊ณ ํ๋ณํ์ ๋ฒ๊ฑฐ๋ก์์ ์ค์ฌ์ค๋ค.
ํ์ ์์ ์ฑ = ์๋ํ์ง ์์ ํ์ ์ ๊ฐ์ฒด๊ฐ ์ ์ฅ๋๋๊ฒ์ ๋ง๊ณ ์ ์ฅ๋ ๊ฐ์ฒด๋ฅผ ๊บผ๋ด์ฌ๋ ์๋์ ํ์ ๊ณผ ๋ค๋ฅธ ํ์ ์ผ๋ก ์๋ชป ํ๋ณํ๋์ด ๋ฐ์ํ ์ ์๋ ์ค๋ฅ๋ฅผ ์ค์ฌ์ค๋ค.
์ง๋ค๋ฆญ ํด๋์ค ์ ์ธ
class Box<T> {
T item;
void setItem(T item){
this.item = item;
}
T getItem() {
return item;
}
}
T : ํ์ ๋ณ์, T๊ฐ ์๋ ๋ค๋ฅธ๊ฒ์ ์ฌ์ฉํด๋ ๋๋ค. ์ด๋ ์์์ ์ฐธ์กฐํ ํ์ ์ ์๋ฏธํ๋ค.
๊ธฐ์กด์๋ Object๋ก ์ฐธ์กฐ๋ณ์๋ฅผ ๋ง์ด ์ฌ์ฉํ๋๋ฐ ๊ทธ๋ก์ธํด ํ๋ณํ์ด ๋ถ๊ฐํผ ํ๋ค ํ๋ ์ด์ Object ๋์ ์ํ๋ ํ์ ์ ์ง์ ํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
ํ์
์ ์ง์ ํด์ฃผ์ง ์์๋
Box b = mew Box(); // T๊ฐ Object๋ก ๊ฐ์ฃผ๋์ด ํ์ฉ๋๋ค.
b.setItem("ABC"); // ๊ฒฝ๊ณ
์์ฒ๋ผ ์ง๋ค๋ฆญ์ด ๋์ ๋๊ธฐ ์ด์ ์ ์ฝ๋์์ ํธํ์ ์ํด ์์ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋๊ฒ์ด ํ์ฉ๋๋ค๋ง, ํ์ ์ ์ง์ ํ์ง ์์ ์์ ํ์ง ์๋ค๋ ๊ฒฝ๊ณ ๊ฐ ํ์๋๋ค.
์ ๋งํ๋ฉด ๋ฐ๋์ ํ์ ์ ์ง์ ํด์ฃผ์
๋งค๊ฐ๋ณ์์์ ์ ์ฌ์ฑ
Box<String>๊ณผ Box<Integer>๋ ์ง๋ค๋ฆญ ํด๋์ค Box<T>์ ์๋ก ๋ค๋ฅธ ํ์ ์ ๋์ ํด ํธ์ถํ ๊ฒ์ผ ๋ฟ, ์ด ๋์ด ๋ณ๊ฐ์ ํด๋์ค๋ฅผ ์๋ฏธํ์ง ์๋๋ค. (๊ฐ์ ํด๋์ค๋ผ๋ ๋ง์ด๋ค.)
์ปดํ์ผ ํ์ ๋๋ค ๋ชจ๋ ์ด๋ค์ ์์ํ์ ์ธ Box๋ก ๋ฐ๋๋ค. ์ง๋ค๋ฆญ ํ์ ์ด ์ ๊ฑฐ๋๋ค๋ ์๋ฏธ์ด๋ค.
์ง๋ค๋ฆญ ํด๋์ค์ ์ ํ
static ๋ฉค๋ฒ์ ํ์ ๋ณ์ T๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
T๋ ์ธ์คํด์ค ๋ณ์๋ก ๊ฐ์ฃผ๋๋๋ฐ static ๋ฉค๋ฒ๋ ์ธ์คํด์ค ๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
static๋ฉค๋ฒ๋ ํ์ ์ด ๋์ผํ ๊ฒ์ด์ฌ์ผ ํ๋ค. ์ด๋ค ๊ฐ์ฒด์์ ํธ์ถํด๋ ๋ชจ๋ ๋์ผํ๊ฒ ๋์ํ๋ฉฐ ๊ณต์ ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์ง๋ค๋ฆญ ํ์ ์ ๋ฐฐ์ด์ ์์ฑํ๋๊ฒ๋ ํ์ฉ๋์ง ์๋๋ค.
๊ทธ ์ด์ ๋ new ์ฐ์ฐ์ ๋๋ฌธ์ธ๋ฐ ์ด ์ฐ์ฐ์๋ ์ปดํ์ผ ์์ ์ ํ์ T๊ฐ ๋ญ์ง ์ ํํ ์์์ผํ๋ค. Box<T>๋ฅผ ์ปดํ์ผ ํ๋ ์์ ์ T๊ฐ ์ด๋ค ํ์ ์ด ๋ ์ง ์์ ์๊ธฐ ๋๋ฌธ์ instanceof ๋ ๊ฐ์ ์ด์ ๋ก ์ฌ์ฉํ ์ ์๋ค
class Box<T> {
T[] itemArr;
T[] toArray() {
T[] tmpArr = new T[itemArr.length]; //์๋ฌ ์ง๋ค๋ฆญ ๋ฐฐ์ด ์์ฑ๋ถ๊ฐ
...
}
}
์ง๋ค๋ฆญ ํด๋์ค์ ๊ฐ์ฒด ์์ฑ๊ณผ ์ฌ์ฉ
Box.java
import java.util.ArrayList;
public class Box<T> {
ArrayList<T> list =new ArrayList<T>();
void add(T item) { list.add(item); }
T get (int i) { return list.get(i); }
ArrayList<T> getList() { return list; }
int size() { return list.size(); }
public String toString() { return list.toString(); }
}
FruitBox.java
public class FruitBox<T> extends Box<T>{
}
main.java
public class Main {
public static void main(String[] args) {
Box<Apple> appleBox = new Box<Apple>();
Box<Fruit> fruitBox = new Box<Fruit>();
appleBox.add(new Apple());
//๋น์ฐํ์ง๋ง Appleํ์
์ผ๋ก ์์ฑํ ์ฐธ์กฐ๋ณ์๋ Apple๊ฐ์ฒด๋ง ๋ฃ์์์๋ค
//appleBox.add(new Grape());
//์์๊ด๊ณ์ ํ์
๋ค ๋ค ๊ฐ๋ฅ, void add(Fruit item)
fruitBox.add(new Fruit());
fruitBox.add(new Apple());
fruitBox.add(new Grape());
//์ฐธ์กฐ๋ณ์์ ์์ฑ์์ ๋์
๋ ํ์
์ด ์ผ์นํด์ผํ๋ค. ์ผ์นํ์ง ์์ผ๋ฉด ์๋ฌ
//Box<Grape> appleBox2 = new Box<Apple>();
//์์๊ด๊ณ์ ์๋๋ผ๋ ์๋ฌ
//Box<Fruit> FruitBox = new Box<Apple>();
//์ง๋ค๋ฆญ ํด๋์ค๊ฐ ์์๊ด๊ณ์ ์๋๊ฑด ๊ด์ฐฎ๋ค
Box<Apple> appleBox1 = new FruitBox<Apple>();
}
}
class Fruit {
public String toString(){
return "Fruit";
}
}
class Grape extends Fruit{
public String toString(){
return "Grape";
}
}
class Apple extends Fruit{
public String toString(){
return "Apple";
}
}
class Toy {
public String toString(){
return "Toy";
}
}
์ ํ๋ ์ง๋ค๋ฆญ ํด๋์ค
๋งค๊ฐ๋ณ์ T์ ์ง์ ํ ์ ์๋ ํ์ ์ ์ข ๋ฅ๋ฅผ ์ ํํ ์ ์๋ ๋ฐฉ๋ฒ
์ ํํ์ง ์์ผ๋ฉด ๋ชจ๋ ์ข ๋ฅ์ ํ์ ์ด ์ง์ ๋๊ธฐ ๋๋ฌธ์ fruitBox์ Toy๋ฅผ ๋ด์์๋ ์๋ค
extends ์ฌ์ฉ
FruitBoxExtendsFruit.java
/**
* ์ ํ๋ ์ง๋ค๋ฆญ ํด๋์ค
* Fruitํ์
๋ง ํ์
๋งค๊ฐ๋ณ์ T์ ์ง์ ํ ์ ์๋ค
* ๋คํ์ฑ์์ ์กฐ์ํ์
์ฐธ์กฐ๋ณ์๋ก ์์ํ์
๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํฌ ์ ์๋๊ฒ ์ฒ๋ผ Fruit์ ๊ทธ ์์ ํ์
๊น์ง ๊ฐ๋ฅํ๋ค
* @param <T>
*/
public class FruitBoxExtendsFruit<T extends Fruit> extends Box {
ArrayList<T> list = new ArrayList<T>();
}
main.java
FruitBoxExtendsFruit<Apple> appleFruitBox = new FruitBoxExtendsFruit<Apple>();
//Fruitํด๋์ค์ ์์๋ค๋ง ๋ด์ ์ ์๋ค๋ ์ ์ฝ์ด ์ถ๊ฐ๋์ด Toy๋ ๋ชป๋ด์
//FruitBox2<Toy> toyFruitBox = new FruitBox2<Toy>();
//์ฌ์ ํ ์์๊ด๊ณ์ ํ์
๋ค ๋ค ๊ฐ๋ฅํจ
FruitBoxExtendsFruit<Fruit> fruitBoxExtendsFruit = new FruitBoxExtendsFruit<Fruit>();
fruitBoxExtendsFruit.add(new Apple());
fruitBoxExtendsFruit.add(new Grape());
Fruit์ Eatable ์ธํฐํ์ด์ค ๊ตฌํ
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํ๋ค๋ ์ ์ฝ์ด ์กด์ฌํ๋ค๋ฉด
Eatable.interface
public interface Eatable {
}
/**
* ์ธํฐํ์ด์ค๋ ํ์
๋งค๊ฐ๋ณ์ T์ ์ง์ ํ ์ ์๋ค
* extends๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ฃผ์
* Fruit์ ์์์ด๋ฉด์ Eatable์ธํฐํ์ด์ค๋ ๊ตฌํํด์ผํ๋ค๋ฉด ๋ค์์ฒ๋ผ & ๊ธฐํธ๋ก ์ฐ๊ฒฐํ๋ค
* @param <T>
*/
public class FruitBoxExtendsFruitandEatable<T extends Fruit & Eatable> extends Box {
ArrayList<T> list = new ArrayList<T>();
}
Fruit์ ์์์ด๋ฉด์ Eatable์ ๊ตฌํํ ํด๋์ค
public class FruitImplEatable extends Fruit implements Eatable{
public String toString(){
return "Fruit";
}
}
main.java
//Fruit์ ์์์ด๋ฉด์ Eatable์ ๊ตฌํํ๋ FruitImplEatableํด๋์ค๋ฅผ ํ์
์ผ๋ก ์ฌ์ฉ๊ฐ๋ฅํ๋ค
FruitBoxExtendsFruitandEatable<FruitImplEatable> fr = new FruitBoxExtendsFruitandEatable<FruitImplEatable>();
fr.add(new Apple());
fr.add(new Grape());
์์ผ๋ ์นด๋
<? extends T>
: ์์ผ๋ ์นด๋์ ์ํ ์ ํ, T์ ๊ทธ ์์๋ค๋ง ๊ฐ๋ฅ<? super T>
: ์์ผ๋ ์นด๋์ ํํ ์ ํ, T์ ๊ทธ ์กฐ์๋ค๋ง ๊ฐ๋ฅ<?>
: ์ ํ ์์ ๋ชจ๋ ํ์ ์ด ๊ฐ๋ฅํ๋ค<? extends Object
์ ๋์ผ
์ธ์ผ๋ ์นด๋์ ํ์์ฑ
Juice.java
public class Juice {
String name;
Juice(String name) {
this.name = name + "Juice";
}
public String toString(){ return name; }
}
Juicer.java
public class Juicer {
/**
* ์์ผ๋ ์นด๋๋ฅผ ์ฌ์ฉํ๋ฉด FruitBox<Fruit>๋ฟ๋ง ์๋๋ผ Fruit<Apple>๋ฑ ์์๋ค๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
*/
static Juice makeJuice(FruitBox<? extends Fruit> box) {
String tmp = "";
for (Fruit f : box.getList()){
tmp += f + " ";
}
return new Juice(tmp);
}
/**
* ๋งค๊ฐ๋ณ์์ ๊ณผ์ผ๋ฐ์ค๋ฅผ ๋์
ํ๋ฉด ์ฃผ์ค๋ฅผ ๋ง๋ค์ด ๋ฐํํ๋ Juicerํด๋์ค
* ์ด ํด๋์ค๋ ์ง๋ค๋ฆญํด๋์ค๋ ์๋๊ณ static ์ ํ์
๋งค๊ฐ๋ณ์ T๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
* ๊ทธ๋์ Apple์ ํ์
์ผ๋กํ FruitBox๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ด์ฃผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์..
*/
// static Juice makeJuice(FruitBox<Fruit> box) {
//
// String tmp = "";
// for (Fruit f : box.getList()){
// tmp += f + " ";
// }
// return new Juice(tmp);
// }
/**
* Appleํ์
์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ค๋ฒ๋ก๋ฉ ์ฝ๋๋ฅผ ์ง๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
* ์ง๋ค๋ฆญ ํ์
์ด ๋ค๋ฅธ๊ฒ ๋ง์ผ๋ก๋ ์ค๋ฒ๋ก๋ฉ์ด ์ฑ๋ฆฝํ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค.
* ์ง๋ค๋ฆญ ํ์
์ ์ปดํ์ผ ํ ๋๋ง ์ฌ์ฉํ๊ณ ์ ๊ฑฐํ๊ธฐ ๋๋ฌธ์ ์ด๋ ๋ฉ์๋ ์ค๋ณต ์ ์๊ฐ ๋์ด ์๋ฌ๊ฐ ๋๋ค.
*/
// static Juice makeJuice(FruitBox<Apple> box) {
//
// String tmp = "";
// for (Fruit f : box.getList()){
// tmp += f + " ";
// }
// return new Juice(tmp);
// }
}
main.java
FruitBox<Fruit> fruit_FruitBox = new FruitBox<Fruit>();
FruitBox<Apple> apple_FruitBox = new FruitBox<Apple>();
//Fruit์ ๊ทธ ์์์ธ Apple๋ ๊ฐ๋ฅํจ
System.out.println("Juicer.makeJuice(fruit_FruitBox) = " + Juicer.makeJuice(fruit_FruitBox));
System.out.println("Juicer.makeJuice(apple_FruitBox) = " + Juicer.makeJuice(apple_FruitBox));
Collections.sort()๋ฅผ ์ด์ฉํ ์ ๋ ฌ
Fruit
class Fruit {
String name;
int weight;
Fruit(String name, int weight){
this.name = name;
this.weight = weight;
}
public String toString(){
return name+"{"+weight+"}";
}
}
public class FruitComp implements Comparator<Fruit> {
public int compare(Fruit t1, Fruit t2){
return t1.weight = t2.weight;
}
}
Apple
class Apple extends Fruit {
Apple(String name, int weight) {
super(name, weight);
}
public String toString(){
return name+"{"+weight+"}";
}
}
public class AppleComp implements Comparator<Apple> {
public int compare(Apple t1, Apple t2){
return t2.weight - t1.weight;
}
}
main.java
public class Main {
public static void main(String[] args) {
FruitBox<Apple> appleBox = new FruitBox<Apple>();
appleBox.add(new Apple("GreenApple", 300));
appleBox.add(new Apple("GreenApple", 100));
appleBox.add(new Apple("GreenApple", 200));
Collections.sort(appleBox.getList(), new AppleComp());
System.out.println("appleBox = " + appleBox);
Collections.sort(appleBox.getList(), new FruitComp());
System.out.println("appleBox = " + appleBox);
}
}
์ด๋ Collections.sort()๋ฅผ ์ด์ฉํด appleBox์ ๋ด๊ธด ๊ณผ์ผ์ ๋ฌด๊ฒ๋ณ๋ก ์ ๋ ฌํ๋ ๊ฒ์ด๋ค. Collections์ ์ ์ธ๋ถ๋ ๋ค์๊ณผ ๊ฐ๋ค
static <T> void sort (List<T> list, Comparator<? super T> c)
์ด๋ ์ง๋ค๋ฆญ ๋ฉ์๋์ด๋ค. list๋ ์ ๋ ฌํ ๋์, c๋ ์ ๋ ฌํ ๋ฐฉ๋ฒ์ด ์ ์ ๋ Comparator์ด๋ค. ์ง๊ธ ์์ผ๋ ์นด๋๊ฐ ์ฌ์ฉ๋์ด new FruitComp
๋ก๋ Apple์ ์ ๋ ฌํ ์ ์๋ค. ๋ง์ผ ์์ผ๋ ์นด๋๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด Apple์ Comparator<Apple>
๋ก Grape๋ Comparator<Grape>
๋ก๋ง ์ ๋ ฌํ ์ ์์๊ฒ์ด๋ค. ์๋ก์ด ๊ณผ์ผ์ด ์๊ธธ๋๋ง๋ค ~Comp.java๋ฅผ ๋ง๋ค์ด์ค์๋ ์์ผ๋ ์์ผ๋์นด๋๋ก ํํ ์ ํ์ ํด์ฃผ๋๊ฒ์ด๋ค.
T์ Apple์ด ๋์ ๋๋ฉด ๋ค์๊ณผ ๊ฐ๋ค
static <Apple> void sort (List<T> list, Comparator<? super Apple> c)
Comparator<? super Apple>
์ Comparator์ ํ์
๋งค๊ฐ๋ณ์๋ก Apple๊ณผ ๊ทธ ์กฐ์์ด ๊ฐ๋ฅํ๋ค๋๊ฑฐ๋ค. ๊ทธ๋์ new FruitComp
๋ก ๋ค๋ฅธ ๊ณผ์ผ๋ค๋ ์ ๋ ฌ๊ฐ๋ฅํ๋ค.
๋ชฐ๋ก ๊ณผ์ผ์ ์กฐ์์ Fruit๋ก ์์ํด์ฃผ์ด์ผ ํ๋ค.
์ง๋ค๋ฆญ ๋ฉ์๋
๋ฉ์๋ ์ ์ธ๋ถ์ ์ง๋ค๋ฆญ ํ์ ์ด ์ ์ธ๋ ๋ฉ์๋๋ฅผ ์ง๋ค๋ฆญ ๋ฉ์๋๋ผ ํ๋ค.
์ง๋ค๋ฆญ ํด๋์ค์ ์ ์๋ ํ์ ๋งค๊ฐ๋ณ์์ ์ง๋ค๋ฆญ ๋ฉ์๋์ ์ ์ ๋ ๋งค๊ฐ๋ณ์๋ ์ ํ ๋ณ๊ฐ์ ๊ฒ์ด๋ค.
static ๋ฉค๋ฒ์๋ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ง๋ง ๋ฉ์๋์ ์ง๋ค๋ฆญ ํ์ ์ ์ ์ธํ๊ณ ์ฌ์ฉํ๋ ๊ฒ์ ๊ฐ๋ฅํ๋ค.
์ด ํ์ ๋งค๊ฐ๋ณ์๋ ๋ฉ์๋ ๋ด์์๋ง ์ง์ญ์ ์ผ๋ก ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ ์ง์ญ๋ณ์๋ฅผ ์ ์ธํ ๊ฒ๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉด ์ดํดํ๊ธฐ ์ฝ๋ค. ๊ทธ๋ ๊ธฐ์ static ์ด๋ ์๋๋ ์๊ด์ด ์๋ค.
makeJuice๋ฅผ ์ง๋ค๋ฆญ ๋ฉ์๋๋ก ๋ฐ๊พธ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
static <T extends Fruit>Juice makeJuice (FruitBox<T> box) {
String tmp = "";
for(Fruit f : box.getList()) {
tmp += f + " ";
}
return new Juice(tmp);
}
์ด ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ์๋์ ๊ฐ์ด ํ์ ๋ณ์์ ํ์ ์ ๋์ ํด์ผ ํ๋ค.
ํ์ง๋ง ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์ ์ถ์ ํ ์ ์์ด ์๋ตํด๋ ๋๋ค.
ํ ๊ฐ์ง ์ฃผ์ํ ์ ์ ์ง๋ค๋ฆญ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋์ ๋ ํ์ ์ ์๋ตํ ์ ์๋ ๊ฒฝ์ฐ์๋ ์ฐธ์กฐ๋ณ์๋ ํด๋์ค ์ด๋ฆ์ ์๋ตํ ์ ์๋ค๋ ๊ฒ์ด๋ค. ๋จ์ง ๊ธฐ์ ์ ์ธ ์ด์ ์ด๋ฏ๋ก ์ง์ผ์ผํ๋ค.
FruitBox<Fruit> fruit_FruitBox = new FruitBox<Fruit>();
FruitBox<Apple> apple_FruitBox = new FruitBox<Apple>();
//Fruit์ ๊ทธ ์์์ธ Apple๋ ๊ฐ๋ฅํจ
System.out.println("Juicer.makeJuice(fruit_FruitBox) = " + Juicer.<Fruit>makeJuice(fruit_FruitBox));
System.out.println("Juicer.makeJuice(apple_FruitBox) = " + Juicer.<Apple>makeJuice(apple_FruitBox));
๋งค๊ฐ๋ณ์์ ํ์ ์ด ๋ณต์กํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
static void printAll (ArrayList<? extends Product> list,
ArrayList<? extends Product> list2) {
for (Unit u : list){
System.out.println(u);
}
}
//์์ ์ฝ๋๋ฅผ ๊ฐ๋ตํ๊ฒ ๋ณ๊ฒฝ
static <T extends Product> void printAll (ArrayList<T> list,
ArrayList<T> list2) {
for (Unit u : list){
System.out.println(u);
}
}
์ง๋ค๋ฆญ ํ์
์ ํ๋ณํ
์ง๋ค๋ฆญ ํ์ ๊ณผ ์ง๋ค๋ฆญ ํ์ ์ด ์๋ ํ์ ๊ฐ์ ํ๋ณํ์ ํญ์ ๊ฐ๋ฅํ๋ค.
ํ์ง๋ง ๋์ ๋ ํ์ ์ด ๋ค๋ฅธ ์ง๋ค๋ฆญ ํ์ ๊ฐ์๋ ํ๋ณํ์ด ๋ถ๊ฐํ๋ค.
Box<Object> objBox = null;
Box<String> strBox = null;
objBox = (Box<Object>) strBox; //์๋ฌ
//์ด๋ฏธ ์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ฑ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ๋ฐฐ์ ๋ค ์ด๋ ์์ฒ๋ผ ํ๋ณํ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ๊ฐ์ ์ ์ผ๋ก ์๋ ค์ค๋ค.
Box<Object> objBox = new Box<String>();
//ํ์ง๋ง ๋ค์์ ๋ฌธ์ฅ์ ํ๋ณํ์ด ๊ฐ๋ฅํ๋ค
Box<? extends Object> objBox = new Box<String>();
//๋ฐ๋์ ๊ฒฝ์ฐ๋ ์ฑ๋ฆฝํ์ง๋ง ํ์ธ๋์ง ์์ ํ๋ณํ์ด๋ผ๋ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ๋ค.
FruitBox<? extends Fruit> box = null;
Box<Apple> appleBox = (FruitBox<Apple>) box;
Optional ํด๋์ค
public final class Optional<T> {
//EMPTY์ ๋น์ด์๋ Optional ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ์ ์ฅ
private static final Optional<?> EMPTY = new Optional<>();
//<?> -> <? extends Object>๋ฅผ ์ค์ฌ์ด๊ฒ
//<>์์ ์๋ต๋ ํ์
์ <Object>์ด๋ค
//๋ฐ๋ผ์ ํ์ด์ฐ๋ฉด Optional<? extends Object> EMPTY = new Optional<Object>();
private final T value;
//EMPTY๋ฅผ ํ๋ณํํด์ ๋ฐํ
//EMPTY์ ํ์
์ Optional<Object>๊ฐ ์๋ Optional<?>๋ก ํ ์ด์ ๋ Optional<T>๋ก ํ๋ณํ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ง์ฝ Optional<Object>๋ฉด ํ๋ณํ์ด ๋ถ๊ฐ๋ฅํ๋ค.
public static<T> Optional<T> empty() {
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
}
์ ๋ฆฌํ๋ฉด
Optional<Object>
๋ฅผOptional<String>
์ผ๋ก ์ง์ ํ๋ณํ ํ๋๊ฒ์ ๋ถ๊ฐ๋ฅํ์ง๋ง ์์ผ๋ ์นด๋๊ฐ ํฌํจ๋ ์ง๋ค๋ฆญ ํ์ ์ผ๋ก ํ๋ณํ ํ๋ฉด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ด๋ค.์ฐธ๊ณ ๋ก ์์ผ๋ ์นด๋๊ฐ ์ฌ์ฉ๋ ์ง๋ค๋ฆญ ํ์ ๋ผ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ ํ๋ณํ์ด ๊ฐ๋ฅํ๋ค. ๋ค๋ง ๋ฏธํ์ ํ์ ์ผ๋ก ํ๋ณํ ํ๋ ๊ฒ์ด๋ผ๋ ๊ฒฝ๊ณ ๊ฐ ๋ฌ๋ค.
FruitBox<? extends Object> objBox = null;
FruitBox<? extends String> strBox = null;
strBox = (FruitBox<? extends String>) objBox;
objBox = (FruitBox<? extends Object>) strBox;
์ง๋ค๋ฆญ ํ์
์ ์ ๊ฑฐ
์ปดํ์ผ๋ฌ๋ ์ง๋ค๋ฆญ ํ์ ์ ์ด์ฉํด ์์คํ์ผ์ ์ฒดํฌ ํ๋ค ํ์ํ ๊ณณ์ ํ๋ณํ์ ๋ฃ์ด์ค๋ค. ๊ทธ๋ฆฌ๊ณ ์ง๋ค๋ฆญ ํ์ ์ ์ ๊ฑฐํ๋ค.
์ฆ ์ปดํ์ผ๋ .class ํ์ผ์๋ ์ง๋ค๋ฆญ ํ์ ์ ๋ํ ์ ๋ณด๊ฐ ์๋ค.
๊ทธ ์ด์ ๋ ์ง๋ค๋ฆญ์ด ๋์ ๋๊ธฐ ์ด์ ์ ์์ค์ฝ๋์์ ํธํ์ฑ์ ์ ์งํ๊ธฐ ์ํด์์ด๋ค.
์ง๋ค๋ฆญ ํ์ ์ ์ ๊ฑฐ ๊ณผ์ ์ ๊ฝค ๋ณต์กํ๋ค. ๊ธฐ๋ณธ์ ์ธ ์ ๊ฑฐ ๊ณผ์ ๋ง ์์๋ณด์.
์ง๋ค๋ฆญ ํ์ ์ ๊ฒฝ๊ฒ๋ฅผ ์ ๊ฑฐํ๋ค. ์ง๋ค๋ฆญ ํ์ ์ด <T extends Fruit> ๋ผ๋ฉด T๋ Fruit๋ก ์นํ๋๊ณ <T>์ธ ๊ฒฝ์ฐ๋ Object๋ก ์นํ๋๋ค. ํด๋์ค์์ ์ ์ธ์ ์ ๊ฑฐ๋๋ค.
์ง๋ค๋ฆญ ํ์ ์ ์ ๊ฑฐํ ํ์ ํ์ ์ด ์ผ์นํ์ง ์์ผ๋ฉด ํ๋ณํ์ ์ถ๊ฐํ๋ค.
static Juice makeJuice (FruitBox<? extends Fruit> box) {
String tmp = "";
for(Fruit f : box.getList()) {
tmp += f + " ";
}
return new Juice(tmp);
}
์์๊ฐ์ด ์์ผ๋ ์นด๋๊ฐ ํฌํจ๋์ด ์๋ ๊ฒฝ์ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ ํ ํ์ ์ผ๋ก์ ํ๋ณํ์ด ์ถ๊ฐ๋๋ค.
static Juice makeJuice (FruitBox box) {
String tmp = "";
Iterator it = box.getList().iterator();
while(it.hasNext()) {
tmp += (Fruit)it.next() + " ";
}
return new Juice(tmp);
}
Last updated