Source: 1190000002646193
Generic class
Public classContainer<k, v> { PrivateK Key; PrivateV value; PublicContainer (k K, v V) {key=K; Value=v; } PublicK GetKey () {returnkey; } Public voidSetkey (K key) { This. Key =key; } PublicV GetValue () {returnvalue; } Public voidSetValue (V value) { This. Value =value; }}
Generic interface
Public Interface Generator<t> { public T Next ();}
Public class Implements Generator<string> { privatenew string[]{"Apple", "Banana", "Pear"}; @Override public String Next () { new Random (); return fruits[rand.nextint (3)];} }
Generic methods (a basic principle: use generic methods as much as possible )
Public Static void out (T-t) { System.out.println (t); }
Type wildcard character (in common method, used when no generic method is used)
Upper limit: <? Extends T>? is a subclass of T and T
Lower limit: <? Super T>? is the parent class of T and T
Java generics: Generic classes, generic interfaces, and generic methods