Java basics-Generic parameters of generics

Source: Internet
Author: User

There are 3 commonly used parameters for a generic mechanism:

“?” Represents any type. If only <?&gt is specified, and without extends, then the default is to allow any class. The extends keyword declares the upper bounds of the type, indicating that the parameterized type may be the type specified, or a subclass of this type. The Super keyword declares the lower bound of the type, indicating that the parameterized type may be the type specified, or the parent type of this type, until the object premise fruit is a superclass of apple and orange. This chapter analyzes the meaning and difference of generic parameters by Java code 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);   failed to compile          Object o = list.get (0);           SYSTEM.OUT.PRINTLN (o);         //List.add (fruit); This sentence cannot be compiled             //List.add (New Object ());  //cannot compile          list.add (null);//This sentence can be compiled because null has no type information       } &nbsp ;  in the above example, it is clear that the records obtained from the list must be fruit, but it is not possible to determine whether the apple,list can add any other parameters except for passing in null, and look at the following example before analyzing the cause: [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 ());  }   from the above code can be seen: The extends keyword declares the upper bounds of the type, indicating that the parameterized type may be the type specified, or a subclass of this type, list< Extends fruit> can point to arraylist<fruit> (), arraylist<apple> (), arraylist<orange> (), and so on. Now if the passed-in parameter list is new Arraylist<apple> (), then you add fruit to this list, you will be sure to give an error. While reading the data, regardless of the list is what type, read the data must be fruit, while fruit is also an object. With the above analysis, it is not possible to add records directly to the list in the inheritance extends, so how to add records to the extends decorated lists, you can use the generic method to achieve the purpose: [Java]  public <t extends Fruit > void ExtendType2 (list<t> List, T date) {           List.add (date),    & nbsp The  }    call example is as follows: [Java]  public void TestExtendType2 () {           list< Apple> List = new Arraylist<apple> ();            extendType2 (list,new Apple ());       }  super parameter super use not much, 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); Cannot compile            List.add (Apple);  //     list.add (new Object ());  //cannot compile  //     list.add (new Fruit ());  //cannot compile     }     in the above code, the records read from the list cannot be determined to be of any type (except object), and list cannot add any type of object except for Apple addition ( Including object). Look at the following code before analyzing the cause: [Java]  public void Testsupertype () {       list<? super apple> List = new Arra Ylist<apple> ();      &NBsp list<? Super Apple> List2 = new arraylist<fruit> ();       Supertype (List2, New Apple ());   &NBSP;} The  super keyword declares the lower bound of the type, indicating whether the parameterized type is the specified type, or the parent type of this type, until object. list<? Super Apple > can refer to  list<apple&gt, or list<fruit&gt, so there is no guarantee that the list is Apple, but it is guaranteed that all lists refer to Apple's parent class , it is no problem to add Apple to this list, and obviously you add object, Fruit object to List<fruit>, list<apple>, and you must fail to add apple.? Represents any type.? can be seen as? extends the abbreviation for 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 () {        /**   &nbs P      *? Can be seen as? Extends Object's 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 refer to any type of  List<, whereas object is the ancestor of all objects, this List can only add objects of type object. Extends and super meaning memory skills? Extends a: Inherit A, is it a subclass, or? Super B:    ? is the parent of B, or is B itself?

Java Fundamentals-Generic parameters of 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.