Java covariant inversion, PECS

Source: Internet
Author: User

    public static void Main (string[] args) {//Object <-Fruit <-Apple <-redapple System.out.pri        Ntln (Apple.class.isAssignableFrom (Fruit.class));        System.out.println (Fruit.class.isAssignableFrom (Fruit.class));  System.out.println (Fruit.class.isAssignableFrom (Apple.class));        Apple is fruit subclass//covariant list<apple> apples = new arraylist<apple> (); list<?        Extends fruit> listfruits = apples;//listfruits.add (new Apple ());//Listfruits.add (New Redapple ());//  Listfruits.add (New Fruit ()); ERROR: cannot be used for one?        Any value is written in the extends data structure.        Fruit f = listfruits.get (0);        Inverter list<fruit> fruits = new arraylist<fruit> (); list<?   Super Apple> List2 = fruits;//list2.add (new Fruit ());        ERROR List2.add (New Apple ());   List2.add (New Redapple ());//Fruit F = list2.get (0); ERROR/* Access principle and pecs rule summary? Extends and the? SuperWildcard character, we can draw the following conclusions: If you want to get data from a data type, use the? Extends wildcard if you want to write an object into a data structure, use the?        Super Wildcard If you want to save and take it, don't use wildcard characters.        This is the principle of access that Maurice Naftalin in his book Java Generics and collections, and the Joshua Rule Bloch effective in his book Pecs java.        Bloch reminded that this pecs refers to "Producer Extends, Consumer Super", this is more easy to remember and use. * */    }

Http://www.cnblogs.com/softidea/p/4106659.html

Java covariant inversion, PECS

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.