zoukankan      html  css  js  c++  java
  • Java PECS

    tl;dr: "PECS" is from the collection's point of view. If you are only pulling items from a generic collection, it is a producer and you should use extends; if you are only stuffing items in, it is a consumer and you should use super. If you do both with the same collection, you shouldn't use either extends or super.


    Suppose you have a method that takes as its parameter a collection of things, but you want it to be more flexible than just accepting a Collection<Thing>.

    Case 1: You want to go through the collection and do things with each item.
    Then the list is a producer, so you should use a Collection<? extends Thing>.

    The reasoning is that a Collection<? extends Thing> could hold any subtype of Thing, and thus each element will behave as a Thing when you perform your operation. (You actually cannot add anything to a Collection<? extends Thing>, because you cannot know at runtime which specificsubtype of Thing the collection holds.)

    Case 2: You want to add things to the collection.
    Then the list is a consumer, so you should use a Collection<? super Thing>.

    The reasoning here is that unlike Collection<? extends Thing>Collection<? super Thing> can always hold a Thing no matter what the actual parameterized type is. Here you don't care what is already in the list as long as it will allow a Thing to be added; this is what ? super Thingguarantees.

    参考:

    https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super

  • 相关阅读:
    Module:template
    Grunt:GruntFile.js
    Grunt:常见错误
    架构:架构-1
    架构:目录
    DS:template
    Jasper:用户指南 / 设备 / 生命周期管理 / SIM 卡状态
    Jasper-Api:接口测试
    linux服务之git
    Java实现 洛谷 P1487 陶陶摘苹果(升级版)
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/10876784.html
Copyright © 2011-2022 走看看