38 _ wildcard extended application, 38 wildcard

Source: Internet
Author: User

38 _ wildcard extended application, 38 wildcard

  • Problem:
    • Define a method to print all data in a set of any parametric types. How can this method be defined?
  • Incorrect Method

        
Public static void printCollection (Collection <Object> cols) {for (Object obj: cols) {System. out. println (obj);}/** cols. add ("string"); // Yes, cols = new HashSet <Date> (); // an error is reported! */}

 

  • The correct method:

      
Public static void printCollection (Collection <?> Cols) {for (Object obj: cols) {System. out. println (obj);}/** cols. add ("string"); // error, because he does not know that his future match must be Stirng cols. size (); // yes. This method has nothing to do with type parameters. cols = new HashSet <Date> (); // yes, it can be used with Collection <?> Draw equal sign */}
  • Summary:
    • Use? Wildcards can reference other parameterized types ,? Variables defined by Wildcards are mainly used as references. You can call Methods unrelated to parameterization, rather than methods related to parameterization.
  

In, the Red Cross method is related to the type, when using wildcard? Cannot be called.

Collection <?> A can match any parameterized type, but only later will know what type is matched,

A = new ArrayList <Integer> (); and a = new ArrayList <String>. add (new Date); or. add ("abc"); none of them work.

 

Objects in Cols <Object> only indicate that the parameters received by methods in the Cols <Object> instance Object are

Cols <Object> is a specific type, and new HashSet <Date> is also a specific type. The two are not compatible.

 

 

 

 

  • In generics? Wildcard Extension
    • Upper boundary of a wildcard

      • Correct Vector <? Extends Number> x = new Vector <Integer> ();

      • Error Vector <? Extends Number> x = new Vector <String> ();

    • Limit the bottom boundary of a wildcard

      • Correct Vector <? Super Integer> x = new Vector <Number> ();

      • Error Vector <? Super Integer> x = new Vector <Byte> ();

    • Prompt

      • Wildcard characters are always included

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.