Packagegeneric;Importjava.util.ArrayList;Importjava.util.List;/***--wildcard, type indeterminate, used to declare a variable | On the parameter * cannot be used in: * 1, create object * 2, create generic class, generic method, generic interface on **/ Public classWildcardtest { Public Static voidMain (string[] args) {//Statementlist<?> list =NewArraylist<integer>(); List=NewArraylist<string>(); List=NewArraylist<object>(); Test (list); //compile error, cannot create object//list = new arraylist<?> (); } Public Static voidTest (list<?>list) { } //? cannot be used on generic methods /*Public static <?> void Test2 (list<?> List) {}*/ //? Cannot create generic class /*class test<?>{}*/}
Of course it doesn't make sense to use it, it's like an object.
Generic upper Limit extends:
Several classes that have an inheritance relationship:
Package generic; /** * Inheritance chain: * Object * / * Apple Pear * /* fujiapple * @author */ Public class Fruit {} class extends fruit{ }classextends fruit{ }classextends apple{ }
Example program:
Packagegeneric;Importjava.util.ArrayList;Importjava.util.List;/*** extends: The upper limit of generics represents <=, subclass * 1, generally used to restrict operation * 2, can not be used on adding data, generally read operation * 3, rule: * encountered list<? extends fruit> what kind of Data can be put up: * list<fruit> * list<apple> * LIST<? Extends apple> * cannot be stored: * list<?> * LIST<? Extends object> *@authorAdministrator **/ Public classExtendstest { Public Static voidMain (string[] args) {//extends is the upper limit, the type limit for list is fruittest<fruit> T1 =NewTest<fruit>(); Test<Apple> t2 =NewTest<apple>(); Test<Pear> t3 =NewTest<pear>(); //Calling Methodslist<?extendsFruit> List1 =NewArraylist<fruit>(); Test (LIST1); List<Fruit> List2 =NewArraylist<fruit>(); Test (LIST2); List<Apple> List3 =NewArraylist<apple>(); Test (LIST3); List<FujiApple> List4 =NewArraylist<fujiapple>(); Test (LIST4); List<?extendsApple> list7 =NewArraylist<fujiapple>(); Test (LIST7); //error: Because of the equivalent of extends Object, the range exceeds fruitList<?> LIST5 =NewArraylist<object>();//test (LIST5);list<?extendsObject> List6 =NewArraylist<object>();//test (LIST6); } //? extends Fruit Public Static voidTest (LIST<?extendsFruit>list) { } //Inner class Static classTest<textendsFruit>{ } }Lower limit of generics: Super
Packagegeneric;Importjava.util.ArrayList;Importjava.util.List;/*** Super: The lower bound of generics >= the parent class or itself 1, generally used under the restriction operation * 2, be able to add data on, cannot add parent object * 3, rule:*/ Public classSuperTest { Public Static voidMain (string[] args) {//the incoming list type must be >=apple, which is the parent class of the Apple class or itselfList<apple> List1 =NewArraylist<apple>(); Test (LIST1); List<Fruit> List2 =NewArraylist<fruit>(); Test (LIST2); List<Object> List3 =NewArraylist<object>(); Test (LIST3); //rule Object > Fruit correctlist<?SuperFruit> List4 =NewArraylist<object>(); Test (LIST4); //Apple = Apple is correctlist<?SuperApple> LIST5 =NewArraylist<apple>(); Test (LIST5); //Fujiapple < Apple, compile errorlist<?SuperFujiapple> List6 =NewArraylist<object>(); //test (LIST6); //--- extends object, which may mean the left side of the declaration is the subclass of object, the right is an object, no? List<?> list7 =NewArraylist<object>(); //test (LIST7); } Public Static voidTest (LIST<?SuperApple>list) { //cannot add parent class object, subclass can add, here is not very clear! List.add (NewApple ()); List.add (Newfujiapple ()); //List.add (New Fruit ());//Parent class cannot be added }}Generic nesting:
Student class:
Package generic; Public class Stu<t> { private T score; Public T Getscore () { return score; } Public void SetScore (T score) { this. score = score; } }
School class:
Packagegeneric;/** * @authorAdministrator * *@param<T>*/ Public classSchool<t> { PrivateT Stu; PublicT Getstu () {returnStu; } Public voidSetstu (T stu) { This. Stu =Stu; } }
Test class:
Packagegeneric;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;/*** Generic nesting *@authorAdministrator **/ Public classStuapp { Public Static voidMain (string[] args) {Stu<String> stu =NewStu<string>(); Stu.setscore (Excellent); System.out.println (Stu.getscore ()); //Generic nesting:School<stu<string>> sch =NewSchool<stu<string>>(); Sch.setstu (Stu); Stu=Sch.getstu (); String score=Stu.getscore (); SYSTEM.OUT.PRINTLN (score); //HashMapmap<string,string> map =NewHashmap<string,string>(); Map.put ("A", "Boss"); Map.put ("B", "Dick"); Set<entry<string, string>> entryset =Map.entryset (); for(Entry<string, string>Entry:entryset) {System.out.println (Entry.getkey ()+ "--->" +Entry.getvalue ()); } }}Generics are not polymorphic:
Packagegeneric;Importjava.util.ArrayList;Importjava.util.List; Public classOther { Public Static voidMain (string[] args) {// polymorphicFruit f =NewApple (); //generics are not polymorphic://list<fruit> List = new arraylist<apple> ();//Errorlist<?extendsfruit> list =NewArraylist<apple>(); //generics do not have an array//fruit<string>[] arr = new fruit<string> ();//Error//JDK1.7 Generics SimplificationList<string> List2 =NewArraylist<>(); }}
Java Generics---wildcard, generic nesting