本帖最后由 邓宫财 于 2013-4-5 18:02 编辑  
 
<? super MyClass>  
一般我们能想到的用法是,- void writeTo(List<? super Apple> apples){
 
 - apples.add(new Apple());
 
 - //apples.add(new Fruit());//这样做就类型不安全,违反了静态类型安全。
 
 - }
 
  
- class Apple extends Fruit{}
 
 - class Fruit{}
 
  复制代码 这样使用更安全,更有用。- public class GenericWriting {
 
 -         /**
 
 -          * 这样这个方法就可以将,T放入T的list中也可以放入T的父类的list中。
 
 -          * @param <T>
 
 -          * @param list
 
 -          * @param item
 
 -          */
 
 -         public static <T> void  addWildcard(List<? extends T> list, T item){
 
 -                 list.add(item);
 
 -         }
 
 -         public static void main(String[] args) {
 
 -                 List<Apple> apples = new ArrayList<GenericWriting.Apple>();
 
 -                 List<Fruit> fruit = new ArrayList<GenericWriting.Fruit>();
 
 -                 
 
 -                 addWildcard(apples,new Apple());
 
 -                 addWildcard(fruit,new Apple());
 
 -                 
 
 -         }
 
 -         
 
 -         static class Apple extends Fruit{}
 
 -         static class Fruit{}
 
  
- }
 
  复制代码 这样跟List<? extends Fruit> list = new ArrayList<Apple>();区别太大了,这个一旦定义就只能装入Apple的对象了。 
 
 
 |