2015-04-20 15:36:01 -07:00
|
|
|
//: generics/GenericWriting.java
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class GenericWriting {
|
|
|
|
static <T> void writeExact(List<T> list, T item) {
|
|
|
|
list.add(item);
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
static List<Apple> apples = new ArrayList<>();
|
|
|
|
static List<Fruit> fruit = new ArrayList<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
static void f1() {
|
|
|
|
writeExact(apples, new Apple());
|
2015-04-29 12:53:35 -07:00
|
|
|
writeExact(fruit, new Apple());
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
static <T> void
|
|
|
|
writeWithWildcard(List<? super T> list, T item) {
|
|
|
|
list.add(item);
|
|
|
|
}
|
|
|
|
static void f2() {
|
|
|
|
writeWithWildcard(apples, new Apple());
|
|
|
|
writeWithWildcard(fruit, new Apple());
|
|
|
|
}
|
|
|
|
public static void main(String[] args) { f1(); f2(); }
|
2015-05-05 11:20:13 -07:00
|
|
|
} ///:~
|