/*
*
* Generics and C + + templates have similar ideas
* Generics are object-oriented
*
* Function Basic use format <T> return value function name () {}//can be any uppercase letter is not necessarily a T
* Class Basic use Format class Classname<t> {} and Class Classname<t extend interface & implements>{}
*
* used in the same way as the normal class is used in the form of new only t to have specific classes (not objects) instead
Generic non-call generic methods and generic classes are tested under common conditions (all of which understand that wildcards are not described)
*/
public class Jgnenerics {
static public <g > void Test (G []t) {
for (G m:t) {
System.out.println (m);
}
}
Compare three values and return a maximum value
public static <t extends comparable<t>> t maximum (t X, t y, T Z)
{
T max = x; Assuming X is the initial maximum value
if (Y.compareto (max) > 0) {
max = y; Greater Y
}
if (Z.compareto (max) > 0) {
max = Z; Now Z is bigger.
}
return Max; Returns the largest object
}
public static void Main (String argc[]) {
Integer i[]={10,100,1000};
Test (i);
Maximum (1,2,1);
R<m1> r=new r<m1> ();//This look is perfectly possible.
Mygenerics}
}
Interface generics<g>{
public void Test (G t);
}
Class Mygenerics<t extends generics>{//note there is no <> in the back of this thing or it will be reported type mismatch
}
Class H<string > Implements generics<string>{
public void Test (String a) {}
}
Class m{}
Class M1 extends m{}
Class R<g extends m>{}
Java in the first talk of generics