Java generic wildcard characters and qualification

Source: Internet
Author: User

Extract Notes

A: The generic type's qualification/** Will the hotel staff, cooks, waiters, managers, respectively stored in 3 sets * Define the method, you can traverse 3 sets at the same time, traverse three sets, you can invoke the working method*/   Importjava.util.ArrayList; ImportJava.util.Iterator;  Public classGenerictest { Public Static voidMain (string[] args) {//Create 3 Collection objectsArraylist<chushi> cs =NewArraylist<chushi>(); ArrayList<FuWuYuan> fwy =NewArraylist<fuwuyuan>(); ArrayList<JingLi> JL =NewArraylist<jingli>(); //Each collection stores its own elementsCs.add (NewChushi ("Zhang San", "After kitchen 001")); Cs.add (NewChushi ("John Doe", "After kitchen 002")); Fwy.add (NewFuwuyuan ("Cui Hua", "Service 001")); Fwy.add (NewFuwuyuan ("Sauerkraut", "service 002")); Jl.add (NewJingli ("nickname", "Board of Directors 001", 123456789.32)); Jl.add (NewJingli ("Xiao Qiang", "board 002", 123456789.33)); //arraylist<string> arraystring = new arraylist<string> ();iterator (JL);      Iterator (FWY);    Iterator (CS); }    /** Define the method, you can traverse 3 sets at the same time, traverse three sets at the same time, you can invoke the working method work *? wildcard, iterator It.next () method to take out the object type, how to invoke the Job method cast: It.next (     ) =object o ==> Employee * method Parameters: Control, you can pass the employee object, or you can pass the object of the child class of the employee * Generic qualification of this case, the parent class fixed Employee, but the subclass can be unlimited? *   ? Extends employee limit is the parent class, the upper limit, can pass the employee, pass his child class object *? Super Employee restricts the subclass, lower bound, can pass Employee, pass his parent class object*/     Public Static voidIterator (arraylist<?extendsEmployee>Array) {Iterator<?extendsEmployee> it =Array.iterator ();  while(It.hasnext ()) {//gets the next () data type, what is the employeeEmployee e =It.next ();       E.work (); }    }   }

Java generic wildcard characters and qualification

Related Article

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.