1. 泛型通配符T的使用
public class Apple { private String appleName = "apple_"; private static int index = 0; public String myName() { appleName += index; return appleName; } }
public class Box<T> { private T object; public void set(T inObj) { this.object = inObj; } public T get() { return object; } }
Box<Apple> appleBox = new Box<Apple>(); appleBox.set(new Apple()); Apple theApple = appleBox.get();
2. 多个通配符的使用
public interface SlefMap<K,V> { public K getKey(); public V getValue(); }
public class RealSelfMap<K,V> implements SlefMap<K,V> { private K key; private V value; public RealSelfMap(K theKey,V theValue) { key = theKey; value = theValue; } public K getKey() { return key; } public V getValue() { return value; } }