The inversion of Java generics

Source: Internet
Author: User

In the previous article, "The Java generics are covariant," the following issues are left behind--covariance does not resolve the addition of subtypes to the generic list of the parent type. This article will use contravariance to solve this problem.

Experiment Preparation

Let's start by adding the following method, as shown in Listing 1.

Code Listing 1

    /*** Description: EXP3 use invert <br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramitemList*/     Public voidDoDecorate3 (list<?SuperT>itemList, T t) {         for(inti = 0; I < itemlist.size (); i++) {System.out.println (Itemlist.get (i)); }    }    /*** Description: EXP3 use invert <br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramitemList*/     Public voidAddDecorate3 (list<?SuperT>itemList, T t)    {Itemlist.add (t); }

Grammar list<? Super T> is the inverse of the Java generics, and the Itemlist.add (T) statement in the AddDecorate3 method does not have a covariant compilation problem.

Experiment: Generic Inversion

Now let's try the use of contravariance, as shown in Listing 2.

Code Listing 2

/*** class Name: exp3<br/> * Description: Generic contravariant use <br/> * Author: Chiang <br/> * Created: August 25, 2015 <br/> * * History: (version) Author time comment*/classEXP3 { Public Static voidMain (string[] args) {Decorator<Auction> Decoratora =NewDecorator<auction>(); List<Auction> ListA =NewArraylist<auction>(); Auction Auctionone=NewAuction ("Auctionone"); Auction Auctiontwo=NewAuction ("Auctiontwo");        Decoratora.adddecorate3 (ListA, Auctionone);        Decoratora.adddecorate3 (ListA, auctiontwo);                Decoratora.dodecorate3 (ListA); Decorator<Table> Decoratorb =NewDecorator<table>(); List<Table> Listb =NewArraylist<table>(); Table Tableone=NewTable ("Tableone", 10); Table Tabletwo=NewTable ("Tabletwo", 20);        Decoratorb.adddecorate3 (Listb, Tableone);        Decoratorb.adddecorate3 (Listb, tabletwo);                Decoratorb.dodecorate3 (LISTB); Decorator<Service> DECORATORC =NewDecorator<service>(); List<Service> LISTC =NewArraylist<service>(); Service ServiceOne=NewService ("ServiceOne", "MethodOne"); Service Servicetwo=NewService ("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 would compile an error:

        Decoratora.dodecorate3 (LISTB);        Decoratora.dodecorate3 (LISTC);

Let's put this problem aside for the time being, commenting it out temporarily, adding the code in Listing 3.

Code Listing 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 Listing 3, Decoratora.dodecorate3 (LISTB); Decoratora.dodecorate3 (LISTC); Decoratorb.dodecorate3 (LISTC); Decoratorc.dodecorate3 (LISTB); This four-statement compilation error, which indicates The following declarations are allowed:

list<?SuperAuction> itemList =NewArraylist<object>(); List<?SuperAuction> itemList =NewArraylist<auction>(); List<?SuperTable> itemList =NewArraylist<object>(); List<?SuperTable> itemList =NewArraylist<auction>(); List<?SuperTable> itemList =NewArraylist<table>(); List<?SuperService> itemList =NewArraylist<object>(); List<?SuperService> itemList =NewArraylist<auction>(); List<?SuperService> itemList =NewArraylist<service> ();

And the following is not allowed:

Super New Arraylist<table>(); ListSupernew arraylist<service>(); ListSupernew arraylist<service>(); ListSupernew arraylist<table> ();

Now let's write the following code:

        Super auction> itemList = ListA;         = Itemlist.get (0);         = Itemlist.get (0);         = Itemlist.get (0);         = Itemlist.get (0);         = listd;         = Itemlist.get (0);         = Itemlist.get (0);         = Itemlist.get (0);         = Itemlist.get (0);

In the above code, except for the statement that gets object from Itemlist, it compiles an error, which is why? Since Itemlist is likely to be a arraylist<object>, it is possible that the transition to auction is wrong. Itemlist may be arraylist<object> or ARRAYLIST<AUCTION>, so the transition to table or service must be wrong.

Finally, we add the following code:

        Itemlist.add (new Auction ("Auctionthr"));        Itemlist.add (new Service ("Servicethr", "Methodthr"));        Itemlist.add (new Table ("Tablethr", 10));

There is no problem with the code above, because whether itemlist is arraylist<object> or arraylist<auction>, adding Auction, Table, and service to it conforms to the original principle.

If adding an object to Itemlist is a compilation failure, because itemlist may be arraylist<auction>.

Summarize

If you add a subclass to a generic, you can use contravariance to implement it. If you want to get subclasses from generics, Contravariant has a transformation error, you should use covariance at this time.

The inversion of Java generics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.