JAVA Generics-inverters and java generics
In the previous article "JAVA generic-Change", the following issues are left over: the child types cannot be added to the parent type generic list. This article uses inverters to solve this problem.
Lab preparation
First, add the following method, as shown in code list 1.
Code List 1
/***** Description: Exp3 inverter <br/> * OPERATOR: jiaan. gja <br/> * History: (Version) author time note <br/> * @ param itemList */public void doDecorate3 (List <? Super T> itemList, T t) {for (int I = 0; I <itemList. size (); I ++) {System. out. println (itemList. get (I) ;}}/***** Description: Exp3 inverter <br/> * OPERATOR: jiaan. gja <br/> * History: (Version) author time note <br/> * @ param itemList */public void addDecorate3 (List <? Super T> itemList, T t) {itemList. add (t );}
Syntax List <? Super T> This is a Java generic inverter. The itemList. add (t) Statement in addDecorate3 method does not have any compilation problems.
Experiment: Generic inverters
Now we try to use the inverter, as shown in code list 2.
Code List 2
/***** Class Name: Exp3 <br/> * Description: Generic inverter use <br/> * OPERATOR: Jia An <br/> * creation: august 25, 2015 <br/> ** historical history: (Version) author time note */class Exp3 {public static void main (String [] args) {Decorator <Auction> decoratorA = new Decorator <Auction> (); List <Auction> listA = new ArrayList <Auction> (); Auction auctionOne = new Auction ("auctionOne "); auction auctionTwo = new Auction ("auctionTwo"); decoratorA. addDecorate3 (listA, auctionOne); decoratorA. addDecorate3 (listA, auctionTwo); decoratorA. doDecorate3 (listA); Decorator <Table> decoratorB = new Decorator <Table> (); List <Table> listB = new ArrayList <Table> (); table tableOne = new Table ("tableOne", 10); Table tableTwo = new Table ("tableTwo", 20); decoratorB. addDecorate3 (listB, tableOne); decoratorB. addDecorate3 (listB, tableTwo); decoratorB. doDecorate3 (listB); Decorator <Service> decoratorC = new Decorator <Service> (); List <Service> listC = new ArrayList <Service> (); service serviceOne = new Service ("serviceOne", "methodOne"); Service serviceTwo = new Service ("serviceTwo", "methodTwo"); decoratorC. addDecorate3 (listC, serviceOne); decoratorC. addDecorate3 (listC, serviceTwo); decoratorC. doDecorate3 (listC);/*** test inverter start */decoratorA. doDecorate3 (listB); decoratorA. doDecorate3 (listC );}}
We found that the following two statements may cause compilation errors:
decoratorA.doDecorate3(listB); decoratorA.doDecorate3(listC);
Now let's take a look at this issue, comment them out, and add the code in listing 3.
Code List 3
List<Object> listD = new ArrayList<Object>(); decoratorA.doDecorate3(listD); decoratorA.doDecorate3(listA); decoratorA.doDecorate3(listB); decoratorA.doDecorate3(listC); decoratorB.doDecorate3(listD); decoratorB.doDecorate3(listA); decoratorB.doDecorate3(listB); decoratorB.doDecorate3(listC); decoratorC.doDecorate3(listD); decoratorC.doDecorate3(listA); decoratorC.doDecorate3(listC); decoratorC.doDecorate3(listB);
In code list 3, decoratorA. doDecorate3 (listB); decoratorA. doDecorate3 (listC); decoratorB. doDecorate3 (listC); decoratorC. doDecorate3 (listB); The compilation errors of these four statements indicate that the following statements are allowed:
List<? super Auction> itemList = new ArrayList<Object>();List<? super Auction> itemList = new ArrayList<Auction>();List<? super Table> itemList = new ArrayList<Object>();List<? super Table> itemList = new ArrayList<Auction>();List<? super Table> itemList = new ArrayList<Table>();List<? super Service> itemList = new ArrayList<Object>();List<? super Service> itemList = new ArrayList<Auction>();List<? super Service> itemList = new ArrayList<Service>();
The following is not allowed:
List<? super Auction> itemList = new ArrayList<Table>(); List<? super Auction> itemList = new ArrayList<Service>(); List<? super Table> itemList = new ArrayList<Service>(); List<? super Service> itemList = new ArrayList<Table>();
Now let's write the following code:
List<? super Auction> itemList = listA; Object o = itemList.get(0); Auction a = itemList.get(0); Table t = itemList.get(0); Service s = itemList.get(0); itemList = listD; o = itemList.get(0); a = itemList.get(0); t = itemList.get(0); s = itemList.get(0);
In the above Code, except for the statements used to get objects from itemList, compilation errors occur. Why? Because itemList may be an ArrayList <Object>, transforming to Auction may be incorrect. ItemList may be an ArrayList <Object> or an ArrayList <Auction>. Therefore, transforming to a Table or Service is inevitable.
Finally, add the following code:
itemList.add(new Auction("auctionThr")); itemList.add(new Service("serviceThr", "methodThr")); itemList.add(new Table("tableThr", 10));
The above code is correct, because whether itemList is an ArrayList <Object> or an ArrayList <Auction>, adding Auction, Table, and Service to it meets the original principles.
If the compilation fails to add an Object to the itemList, The itemList may be an ArrayList <Auction>.
Summary
If you add a subclass to the generic type, you can use the inverter. If you want to obtain the subclass from the generic type, the inverter has a transformation error. In this case, you should use the covariant.