Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 782 Bytes

28. 利用通配符来提高API的灵活性.md

File metadata and controls

32 lines (25 loc) · 782 Bytes

PECS

  • producer-extends
  • consumer-super
public class Example {
    
    public static void main(String[] args) {
        //       父 - 子
        // A -> B -> C -> D -> E
        
        // consumer(Collection<? super C> data)
        producer(Lists.<A>newArrayList());
        producer(Lists.<B>newArrayList());
        producer(Lists.<C>newArrayList());
        
        // producer(Collection<? extends C> data)
        consumer(Lists.<C>newArrayList());
        consumer(Lists.<D>newArrayList());
        consumer(Lists.<E>newArrayList());
    }
    
    
    public static void consumer(Collection<? super C> data) {
        // 可写不可读
    }
    public static void producer(Collection<? extends C> data) {
        // 可读不可写
    }
    
}