Java basics-Generic parameters

Source: Internet
Author: User

There are three common parameters for the generic mechanism: "?" Represents any type. If only <?>, Without extends, any class is allowed by default. The extends keyword declares the upper bound of the type, indicating that the parameterized type may be the specified type or a subclass of this type. The super keyword declares the lower bound of the type, indicating that the parameterized type may be the specified type or the parent type of the type until the Object is a superclass of Apple and Orange. This chapter uses java code to analyze the meanings and differences of generic parameters. extends parameters: [java] public void extend (List <? Extends Fruit> list, Fruit fruit) {Fruit now = list. get (0); System. out. println ("now =>" + now); // Apple a = list. get (0); the Object o = list cannot be compiled. get (0); System. out. println (o); // list. add (fruit); // This sentence cannot be compiled // list. add (new Object (); // the list cannot be compiled. add (null); // This sentence can be compiled, because null has no type information} in the above example, it can be clear that the record obtained from the list must be Fruit, however, it cannot be determined that it is Apple. In addition to null, list cannot add any other parameters. Before analyzing the cause, first look at the following example: [java] public void testExtendType () {List <? Extends Fruit> list = new ArrayList <Fruit> (); List <? Extends Fruit> list2 = new ArrayList <Apple> (); List <? Extends Fruit> list3 = new ArrayList <Orange> (); extend (list2, new Apple ();} the code above shows that the extends keyword declares the upper bound of the type, it indicates that the parameterized type may be the specified type or a subclass of this type. List <? Extends Fruit> can point to ArrayList <Fruit> (), ArrayList <Apple> (), and ArrayList <Orange>. If the input parameter list is new ArrayList <Apple> (), you must add Fruit to this list. An error is returned. When reading data, no matter what type of list is, the read data must be Fruit, and Fruit is also an Object. Through the above analysis, we know that it is not feasible to directly add records to the list inherited from extends. Therefore, we can use the generic method to add records to the list modified by extends: [java] public <T extends Fruit> void extendType2 (List <T> list, T date) {list. add (date);} call example: [java] public void testExtendType2 () {List <Apple> list = new ArrayList <Apple> (); extendType2 (list, new Apple ();} super parameter is rarely used. Just a brief introduction. [Java] public void superType (List <? Super Apple> list, Apple apple) {Object o = list. get (0); System. out. println (o); // Apple a = list. get (0); // cannot compile // Apple apple = list. get (0); // the list cannot be compiled. add (apple); // list. add (new Object (); // cannot be compiled // list. add (new Fruit (); // cannot be compiled} in the above Code, the records read from the list cannot be determined to be of any type (except for the Object), list except for the addition of Apple, you cannot add any types of objects (including objects ). Before analyzing the cause, read the following code: [java] public void testSuperType () {List <? Super Apple> list = new ArrayList <Apple> (); List <? Super Apple> list2 = new ArrayList <Fruit> (); superType (list2, new Apple ();} the super keyword declares the lower bound of the type, it indicates that the parameterized type is the specified type, or the parent type of this type, until the Object. List <? Super Apple> can reference List <Apple> or List <Fruit>, so it cannot be guaranteed that the list is Apple, but it can be guaranteed that all lists reference the parent class of Apple, it is no problem to add Apple to this List. Obviously, you must add objects and Fruit objects to List <Fruit> and List <Apple>, only Apple can be added .? Parameter "?" Represents any type .? Can it be viewed? The abbreviation of extends Object. [Java] public void wideCardType (List <?> List, Fruit fruit) {Object o = list. get (0); System. out. println (list. get (0); // list. add (fruit); // cannot compile // list. add (new Object (); // cannot be compiled} [java] public void testWideCardType (){/***? Can it be viewed? Extends Object simplified version */List <?> List = new ArrayList <Fruit> (); List <?> List2 = new ArrayList <Apple> (); List <?> List3 = new ArrayList <Object> (); wideCardType (list, new Fruit ();} List <? Super Object> can reference List <any type>, and the Object is the ancestor of all objects. This list can only add objects of the Object type. What are the meanings of extends and super? Extends :? Inherit ,? Is A subclass of A, or? Is it A itself? Super B :? Is the parent class of B, or? It's bits.

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.