class A<T>{
T item;
void setItem(T item){this.item = item};
T getItem() {return item;}
}
T[] itemArr; ///가능
T[] Arr = new T[10] ///불가능
Box<Fruit> appleBox = new Box<Apple>(); ///error
class FruitBox<T extends Fruit>{}
제네릭 클래스 타입의 매개변수를 사용하는 함수에서 다형성을 주기위해 사용
class Juicer{
static Juice makeJuice(FruitBox<? extends Fruit> box){
String tmp = ";
for(Fruit f : box.getList()) tmp += f + " ";
return new Juice(tmp);
}
}
static <T> void sort(List<T> list, Comparator<? super T> c)
ex)
static Juice makeJuice(FruitBox<? extends Fruit> box){
String tmp = ";
for(Fruit f : box.getList()) tmp += f + " ";
return new Juice(tmp);
}
을 변경하면,
static <T extends Fruit> Juice makeJuice(FruitBox<T> box){
String tmp = ";
for(Fruit f : box.getList()) tmp += f + " ";
return new Juice(tmp);
}