? Extends E;? Super E

Source: Internet
Author: User

? Extends e: Can receive subtypes of type E or E. Ceiling.
? Super E: Can receive the Type E or the parent of E. Lower

    

<? Extends class> is a type of restriction wildcard that can accept all <Class> and Class subtypes. However, the cost of the call is read-only access
  1. list<?    Super fruit> list = new arraylist<apple> ();
  2. List.add (new Apple ()); Can be
  3. List.add (new Fruit ()); Can be
  4. list<?    extends fruit> list = new arraylist<apple> ();
  5. Why can't I join the subclasses of the fruit class and the fruit class in the list because of this?
  6. list<? extends fruit> indicates that the upper limit is Fruit, and the following assignments are legal
  7. list<?    extends fruit> list1 = new arraylist<fruit> ();
  8. list<?    extends fruit> list2 = new arraylist<apple> ();
  9. list<?    extends fruit> list3 = new arraylist<jonathan> ();
  10. If list<? extends fruit> Support the method of the Add method is valid
  11. List1 can add fruit and all fruit subclasses
  12. List2 can add apple and all Apple sub-classes
  13. List3 can add Jonathan and all of Jonathan's subclasses
  14. In that case, the problem will arise.
  15. list<? extends fruit> should hold the object is Fruit subclass, and specifically which one
  16. Subclasses are still unknown, so adding any subclass of fruit will be problematic, because if the add Apple
  17. words, may list<? extends fruit> holds the object is new arraylist<jonathan> ()
  18. Apple's accession is certainly not going to work if if add Jonathan
  19. words, may list<? extends Fruit> holds a subclass of new Arraylist<jonathan > ()
  20. Jonathan's accession is not legal, so list< extends fruit> list cannot be add
  21. and list<? The Super fruit> list indicates that the list holds objects that are Fruit's parent class, and the lower bound is Fruit, so the list of add Fruit or Fruit subclasses is not a problem

? Extends E;? Super E

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.