Generics implement the concept of parameterized types so that code can be applied to multiple types.
1. Generic class
A declared generic type static method cannot be used
class Tools<t>{ private T t; Public void set (T t) { this. T = t; } Public t get () { returnthis. T; }}
2. Generic methods
classGenerictest2<t>{ //using generics defined by a class Public voidprint1 (T t) {SYSTEM.OUT.PRINTLN ("Print1:" +t); } //generics defined using the method Public<E>voidPrint2 (e e) {System.out.println ("Print2:" +e); } //static methods cannot use generic types defined by a class Public Static<E>voidPrint3 (e e) {System.out.println ("Print3:" +e); }}3. Generic interface
//generic interfaceInterfaceInter<t>{ voidShow (T t);}//classInterImpl2ImplementsInter<string>{ Public voidShow (String t) {System.out.println ("Show:" +t); } }//classInterimpl<t>ImplementsInter<t>{ Public voidShow (T t) {SYSTEM.OUT.PRINTLN ("Show:" +t); }}classgenericdemo5{ Public Static voidMain (string[] args) {Interimpl<Integer> i =NewInterimpl<integer>(); I.show (5); //Interimpl i = new Interimpl (); //i.show ("Liu"); }}4. Generic Qualification
extends New extends Person> (); // restrict the objects that store the person or person child class Super New Super Person> (); // restrict the objects that store the person or person parent class
Java Basics Summary Generic