1. Classes, methods on the generics
Generic class-defined generic, valid throughout the class. If it is used by the method, the type of the generic class is fixed after the specific type of operation is explicitly made. To allow different methods to manipulate different types, the types are not yet deterministic. You can then define the generic on the method. /* Special: Static methods do not have access to generics defined on the class. If the application data type for a static method operation is not deterministic, you can define the generic on the method. */class demo<t>{public void Show (T t) {System.out.println ("show:" +t); Public <Q> void print (q q) {System.out.println ("Print:" +q);} Public static <W> void method (W t) {System.out.println ("method:" +t);}}
2. Interface to achieve the interface of the generic
/generics are defined on the interface. Interface inter<t>{void Show (T t);} /*class Interimpl implements Inter<string>{public void Show (String t) {System.out.println ("show:" +t);}} */class interimpl<t> implements inter<t>{public void Show (T t) {System.out.println ("show:" +t);}}
3. A generic qualifier, wildcard ( Note:? Wildcard characters can only be used on a class that has a defined generic , making a generic qualification on that class )
/*? A wildcard character. Can also be understood as placeholders. The qualification of generic type;? Extends e: Can receive subtypes of type E or E. Ceiling.? Super E: Can receive the Type E or the parent of E. Lower limit note:? Wildcards can only be used on classes that already have a generic defined, the generic qualification on that class */class genericdemo6{public static void Printcoll (COLLECTION<? extends person > al) {iterator<? extends person> it = Al.iterator (); while (It.hasnext ()) {System.out.println (It.next (). GetName ());}} public static void Printcoll (Arraylist<?> al)//arraylist al = new arraylist<integer> (); error{iterator<? > it = Al.iterator (); while (It.hasnext ()) {System.out.println (It.next (). toString ());}} }
Class fu<t>
{
}
Class Interimp
{
public void Integer (FU<? extends integer> f) {
}
public void string (FU<? Super String> F)
{
}
}
java-using a generic shape