1.1.A question mark wildcard character
There are three different ways to use the question mark wildcard in a type parameter:
(1) Generic<?> indicates that the type parameter is of any object type.
(2) generic<? Extends B> represents a derived class with a type parameter of b or b .
(3) generic<? Super B> represents a parent class with a type parameter of b or b .
Static classA {}Static classBextendsa{}Static classCextendsb{}Static classGeneric<t> { } Static classholder{//Generic<?> can accept various parameterized types with different type parameters. Public voidWork (generic<?>obj) { } //extends B indicates that a type parameter can be a derived class of B, or B. Public voidWORKFORBC (generic<?extendsB>obj) { } //? Super B indicates that the type parameter can be either B or the parent of B. Public voidWorkforab (generic<?SuperB>obj) { } } Public Static voidMain (string[] args) {Holder Holder=NewHolder ();//Work (generic<a> obj);Holder.work (NewGeneric<a>()); //Work (generic<b> obj);Holder.work (NewGeneric<b>()); //Work (generic<c> obj);Holder.work (NewGeneric<c>()); HOLDER.WORKFORBC (NewGeneric<b>()); HOLDER.WORKFORBC (NewGeneric<c>());//error, A is the parent class of B. //HOLDER.WORKFORBC (New generic<a> ());Holder.workforab (NewGeneric<a>()); Holder.workforab (NewGeneric<b>());//error, C is a derived class of B. //Holder.workforab (New generic<c> ()); } }
Java Important Technology (23) question mark wildcard for generics