First, generic type
1 Packagegenerics;2 /**3 * Generic class, Format: public class name < generic type 1, ...>4 * @authorZHONGFG5 * @date 2015-06-166 * @param<T>7 */8 classStudent<t> {9 Ten PrivateT num; One A PublicT Getnum () { - returnnum; - } the - Public voidsetnum (T num) { - This. num =num; - } + - } + A Public classGenericsclass { at Public Static voidMain (string[] args) { - -student<string> S1 =NewStudent<string>(); -S1.setnum ("001"); -String NUM1 =s1.getnum (); - System.out.println (NUM1); in -System.out.println ("----------------"); to +student<integer> s2 =NewStudent<integer>(); -S2.setnum (2); the intnum2 =s2.getnum (); * System.out.println (num2); $ }Panax Notoginseng}
Ii. generic methods
1 Packagegenerics;2 /**3 * Generic method, public < generic type > return value method Name (generic type)4 * @authorZHONGFG5 * @date 2015-06-166 */7 classActor {8 9 Public<T>voidShow (T t) {Ten One System.out.println (t); A } - - } the - Public classGenericsmethod { - - Public Static voidMain (string[] args) { + -Actor A =NewActor (); +A.show ("Angelababy"); AA.show (26); at } -}
Three, generic interface
1 Packagegenerics;2 /**3 * Generic class, Format: public interface interface name < generic type 1, ...>4 * @authorZHONGFG5 * @date 2015-06-166 * @param<T>7 */8 InterfacePerson<t> {9 Ten Public Abstract voidShow (T t); One } A - classPersonimpl<t>ImplementsPerson<t> { - the @Override - Public voidShow (T t) { - //TODO auto-generated Method Stub - System.out.println (t); + } - } + A Public classGenericsinterface { at Public Static voidMain (string[] args) { - -person<string> P1 =NewPersonimpl<string>(); -P1.show ("Huang Xiaoming"); - -person<integer> P2 =NewPersonimpl<integer>(); inP2.show (38); - } to}
Four, generic high-level wildcard characters
1 Packagegenerics;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 6 /**7 * Generic Advanced (wildcard character)8 *? : Any type, if not explicitly specified, is an object or any Java class.9 *? Extends e: down qualification, E and its subclassesTen * Super E: Limit up, E and its parent class One * @authorZHONGFG A * @date 2015-06-16 - */ - classAnimal { the - } - - classDogextendsAnimal { + - } + A classCatextendsAnimal { at - } - - Public classGenericswildcard { - Public Static voidMain (string[] args) { - in //when a generic is clearly specified, it must be consistent before and after -collection<object> C1 =NewArraylist<object>(); to //collection<object> c2 = new arraylist<animal> (); error, front and back type inconsistent + //collection<object> C3 = New Arraylist<dog> (); ibid. - //collection<object> C4 = new arraylist<cat> (); ibid. the * //? indicates that any type is possible $collection<?> c5 =NewArraylist<object>();Panax Notoginsengcollection<?> C6 =NewArraylist<animal>(); -Collection<?> c7 =NewArraylist<dog>(); thecollection<?> C8 =NewArraylist<cat>(); + A //? Extends e: down qualification, E and its subclasses the //Collection< extends animal> C9 = new arraylist<object> (); error, Object not Animal subclass +collection<?extendsAnimal> C10 =NewArraylist<animal>(); -collection<?extendsAnimal> C11 =NewArraylist<dog>(); $collection<?extendsAnimal> C12 =NewArraylist<cat>(); $ - //? Super E: Limit up, E and its parent class -collection<?SuperAnimal> C13 =NewArraylist<object>(); thecollection<?SuperAnimal> C14 =NewArraylist<animal>(); - //collection< Super animal> C15 = new arraylist<dog> (); error, Dog is not Animal and above typeWuyi //collection< Super animal> C16 = new arraylist<cat> (); Ibid . the } -}
Applications for Java generics