java generics -PECS

 

. 1  Package com.example.base;
 2  
. 3  Import of java.util.ArrayList;
 . 4  Import java.util.List;
 . 5  
. 6  Import com.example.spring.MyLog;
 . 7  / ** 
. 8  * Super producers use Producer Consumer extends extends , consumer using Super
 . 9  * herein in terms of producers and consumers are relatively container
 10  * can provide external data producers, can not write data, data from the assignment operator (parameterized type subclass assignment over the container)
 11  * indicates the consumer can only be written to the vessel, can not be read (Object to be received only)
 12  * here and extends super statement refers to the relationship type and a parameter of type
 13  * as follows: the left side of the equal sign is a type declared type, the right side is a parameterized type
 14  * = new new List intList the ArrayList <Integer> (); <the extends Number The?>
15  *    private List<? super Number> intList2 = new ArrayList<Number>();
16  * @DESC 
17  * @author guchuang
18  *
19  */
20 public class PECS {
21 
22     int int1 = 1;
23     long long1 = 11;
24     Number number1 = 10;
25     
26     private List<Integer> intList = new ArrayList<Integer>();
27     private List<Long> longList = new ArrayList<Long>();
28      Private List <Number The> = numberList new new the ArrayList <Number The> ();
 29      
30      public  static  void main (String [] args) {
 31 is          PECS Pecs = new new PECS ();
 32          pecs.pe ();
 33 is          pecs.cs ( );
 34 is      }
 35      
36      / ** 
37 [       * <? Number the the extends> generic containers declared in this way, any type of data can not be written, only read data
 38       * which specifies the supremum to Number, parameter types (such as: actual new type of container T new ArrayList <T> () ) must be a subclass of Number, the data can be read out more transition Number
 39       * theoretical implications of the data contained within the container may be Number any of the subclass, so you can not add data.
40       * Write your data:
41       * declare a particular type (Number of sub-type) of the container, wherein data is added to the numbers assigned to the container vessel
 42       * Advantages:
 43       * avoid writing data to the caller within this container, the inside of the read-only data
 44 is       * / 
45      public  void PE () {
 46 is          // <? Number the the extends> generic containers declared in this way, any type of data can not be written 
47          List <? the extends Number the> Numbers = new new the ArrayList <Number the> ( );
 48          // following three will cause the compiler to add data given
 49          // foo.add (INT1);
 50          // foo.add (long1);
 51 is          // foo.add (number1);
 52 is          
53 is          //Numbers.get = Number Number (0); 
54 is          
55          intList.add (123 );
 56 is          intList.add (456 );
 57 is          // container having determined the type of the newly created (must be a subtype of Number) numbers assigned to , production data object functions 
58          Numbers = intList;       // new container is parameterized types Integer, is a subclass of Number, it is possible to assign 
59          MyLog.info (Numbers);
 60          Number Number = numbers.get (0) ;     // read-out type is still Number the 
61          MyLog.info ( "the extends from the read Number the:?" + Number the);
 62          a Numbers = longlist;
 63          a Numbers = numberList;
 64-     }
 65      
66      / ** 
67       * Consumer Super
 68       *? Super Number represent all types of data containers are Number or Number of super-type,
 69       * so Number and its sub-types (can be on the transition to Number) can be written, when the type of read can not be determined since only received using Object
 70       * / 
71 is      public  void CS () {
 72          List <? Super Numbers = Number the> new new the ArrayList <Number the> ();
 73 is          numbers.add (INT1);
 74          numbers.add (long1);
 75          numbers.add (number1);
 76          // numbers.add (new new Object ()); compiler given 
77          MyLog.info (Numbers);
78          
79          // Number The n-numbers.get = (0); compiler given 
80          Object n-numbers.get = (0);       // can only receive data Object
 81          // Numbers = intList; given compiler 
82          Numbers = numberList;
 83          Numbers = new new the ArrayList <Object> ();
 84      }
 85 }

 

Guess you like

Origin www.cnblogs.com/gc65/p/11183659.html