In the previous article, "Basic use of Java generics," the following questions are left in this post, where subtypes can also be added to the generics of the parent type, which must be supported by covariance.
Experiment Preparation
Now, on the basis of the decorator type shown in the previous article, add some code, as shown in Listing 1.
Code Listing 1
/*** Description: EXP2 use br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramitemList*/ Public voidDoDecorate2 (list<?extendsT>itemList) { for(inti = 0; I < itemlist.size (); i++) {System.out.println (Itemlist.get (i)); } } /*** Description: EXP2 use <br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramItemList *@paramT*/ Public voidAddDecorate2 (list<?extendsT>itemList, T t) {// Itemlist.add (t); }
You can see that this is almost the same as the Dodecorate and Adddecorate methods in the basic use of Java generics, the only difference is to change the list<t> to List< extends T> this syntax is called covariant. If you have written the above code in the IDE, you must have found the statement Itemlist.add (t) in AddDecorate2, and the error has been compiled. So we will first Itemlist.add (t), this statement temporarily commented out, first to see if DoDecorate2 has reached our expectations.
Experiment two: Generic covariance
Now let's try the use of covariant, called the DoDecorate2 method, as shown in Listing 2.
Code Listing 2
/*** class Name: exp2<br/> * Description: Generic covariant use <br/> * Author: jiaan.gja<br/> * created: August 20, 2015 <br/> * * History: (version) Author time comment*/classEXP2 { Public Static voidMain (string[] args) {Decorator<Auction> Decoratora =NewDecorator<auction>(); List<Auction> ListA =NewArraylist<auction>(); Auction Auctionone=NewAuction ("Auctionone"); Auction Auctiontwo=NewAuction ("Auctiontwo"); Decoratora.adddecorate2 (ListA, Auctionone); Decoratora.adddecorate2 (ListA, auctiontwo); Decoratora.dodecorate2 (ListA); Decorator<Table> Decoratorb =NewDecorator<table>(); List<Table> Listb =NewArraylist<table>(); Table Tableone=NewTable ("Tableone", 10); Table Tabletwo=NewTable ("Tabletwo", 20); Decoratorb.adddecorate2 (Listb, Tableone); Decoratorb.adddecorate2 (Listb, tabletwo); Decoratorb.dodecorate2 (LISTB); Decorator<Service> DECORATORC =NewDecorator<service>(); List<Service> LISTC =NewArraylist<service>(); Service ServiceOne=NewService ("ServiceOne", "MethodOne"); Service Servicetwo=NewService ("Servicetwo", "Methodtwo"); Decoratorc.adddecorate2 (LISTC, ServiceOne); Decoratorc.adddecorate2 (LISTC, servicetwo); Decoratorc.dodecorate2 (LISTC); /** Covariance test started*/Decoratora.dodecorate2 (LISTB); Decoratora.dodecorate2 (LISTC); }}
First, we see that the DoDecorate2 method is at least as effective as the Dodecorate method in Java generics basic usage.
Because the type of the Decoratora is declared to be decorator<auction>, then the DoDecorate2 parameter of the instance Decoratora can be considered as follows:
extends Auction> itemList
The type of LISTB is the type of ARRAYLIST<TABLE>,LISTC, which is arraylist<service>, which means that covariant allows:
extends New Arraylist<auction>(); Listextendsnew arraylist<table>(); Listextendsnew arraylist<service> ();
This means that itemlist can store both auction and table and service. Let's assume that when you want to get a table from Itemlist, the Java compiler suspects that it might actually be a service, so there will be a compilation error. There is no problem if you want to get auction from itemlist, because both table and service can be automatically converted to the parent class auction. These assumptions can be verified in code Listing 3.
Code Listing 3
list<? extends auction> itemList = ListA; Auction a = Itemlist.get (0); Table t = itemlist.get (0); Service s = itemlist.get (0); ItemList = Listb; A = Itemlist.get (0); T = itemlist.get (0); s = itemlist.get (0); ItemList = LISTC; A = Itemlist.get (0); T = itemlist.get (0); s = itemlist.get (0);
Similarly, when you want to add a table to itemlist, the compiler suspects that Itemlist is actually a arraylist<service>, and therefore is not allowed. There is no problem when getting auction, so will adding auction be possible? Since itemlist may be arraylist<service> or ARRAYLIST<TABLE>, this is not practical, according to the conclusions of the previous Java generics basic use. You can verify it with the following code:
New Auction ("Auctionthree"); Itemlist.add (auctionthree); New Auction ("Tablethree"); Itemlist.add (tablethree); New Auction ("Servicethree"); Itemlist.add (servicethree);
So this also illustrates the first time you see the addDecorate2 in the statement Itemlist.add (t), why compile the error. Even so, we still want to be able to add subtypes to the generics of the parent type declaration, what should I do? Java Of course also takes this issue into consideration, see the next "Java generics inversion".
Covariance of Java generics