/*** Program: learnpattern. java * Author: tanx * Create: 4:22:46 **/package pattern; import Java. util. arraylist; import Java. util. list;/***** @ author tanx * @ version 1.0.0 * @ 4:22:46 */public class learnpattern <K, v> {// The recommended naming convention is to use a single uppercase letter name as the type parameter. This is different from the C ++ Convention (see Appendix A: Comparison with C ++ // templates) and reflects the assumption that most generic classes will have a small number of type parameters. For common generic modes, the recommended names are: // K -- keys, such as ing keys. // V -- value, such as the content of list and set, or the value in map. // E -- exception class. // T -- generic // Java Program Type security. By knowing the type restrictions of variables defined using generics, // tanx: The display conventions, instead of being in the programmer's mind. In the comments, the compiler helps us eliminate errors, instead of running bugs // eliminate Source code . This makes Code It is more readable and reduces the chance of errors. // The type check is moved from execution to compilation, which improves reliability and speeds up development. Public void put (K key, V value) {// use the same type defined by the class} public v get (K key) {// use the same type defined by the class return NULL ;} // specify that the input parameter type is the same as the return type. If the class definition is not generic, add <t> before the method name, t can be other letters Public <t> T addtest (T value) {return NULL;} public void testcase1 () {learnpattern <integer, string> BB = new learnpattern <integer, string> (); bb. put (1, "test"); // you must pass the string test = BB. get (1); // No type conversion required} public void testcase2 () {List <integer> intlist = new arraylist <integer> (); intlist. add (2); // list <number> numberlist = intlist; // cannot be converted. generics are not covariant. // numberlist. add (2.0); // if it can be converted, this 2.0 can be added, but the actual intlist cannot add 2.0 list <?> Numberlist = intlist; // can be converted? It is often used to upgrade code to eliminate conversion warnings, but note the following // numberlist. Add (2 );//? Unable to add system. Out. println (numberlist. Get (0 ));//? Can query numberlist. Clear ();//? Can delete} // Multiple Parameter constraints Public <t> T ifthenelse (Boolean B, T first, t second) {return B? First: Second;} public void testcase3 () {string S = ifthenelse (false, "A", "B"); integer I = ifthenelse (false, new INTEGER (1 ), new INTEGER (2); // invalid, not just verbal constraints, comments, or errors when running directly // string S = ifthenelse (B, "Pi ", new float (3.14);} // restriction type // tell others without any errors, in this class, the property test can only be the public class testcase4 <t extends number> {private t test;} Interface collection <v> {Boolean addall (collection <? Extends v> C); // without changing the original semantics, you can add a set and a subclass set Boolean removeall (collection <?> C); // Delete the Mixed collection class} // class Enum <E extends Enum <E> // type parameter E is used in various Enum methods, such as compareto () or getdeclaringclass (). // For the type security of these methods, the enum class must be generic on the enumerated class. /// Enum <E> This is its own, and E extends Enum <E> indicates its own child type. // because the enumeration class implements comparable <E>, // public final int compareto (e o) {// Enum Other = (Enum) O; // Enum self = This; // If (self. getclass ()! = Other. getclass () & // optimization // self. getdeclaringclass ()! = Other. getdeclaringclass () // throw new classcastexception (); // return self. ordinal-other. ordinal; //} // public final class <E> getdeclaringclass () {// class clazz = getclass (); // class zuper = clazz. getsuperclass (); // return (zuper = enum. class )? Clazz: zuper; // API description // public final class <E> getdeclaringclass () // return the class object corresponding to the enumeration type of the enumerated constant. // When and only when e1.getdeclaringclass () = e2.getdeclaringclass (), the enumerated types of the two enumerated constants E1 and E2 are the same. (The value returned by this method is different from the value returned by object. getclass () // method. The object. getclass () method is used to enumerate constants of class subjects with specific constants .) // Return: // class object corresponding to the enumerated type of this enumerated constant // public native class <? Super T> getsuperclass (); // local method of class, return the parent public class of this class <? Super integer> testcase5 () {// return the integer's parent class type return NULL ;} // special effect demonstration abstract class Foo <t extends Foo <t> {public abstract t subclassawaredeepcopy ();} class bar extends Foo <bar> {Public Bar subclassawaredeepcopy () {Bar B = new bar ();//... return B ;}} public class testcase5 <t extends number> {Bar B = new bar (); Foo <bar> F = B; // The Child class and parent class assign values to each other bar BB2 = B. subclassawaredeepcopy (); Bar bb3 = f. subclassawaredeepcopy (); // The parent class directly returns the subclass instance without the use of Cast bar1 b1 = new bar1 (); foo1 <foo1> F1 = b1; // The subclass and parent classes are assigned bar1 B12 = b1.subclassawaredeepcopy (); bar1 B13 = (bar1) f1.subclassawaredeepcopy (); // the instance of the subclass returned by the parent class, cast bar2 b2 = new bar2 (); foo2 F2 = B2; // The subclass and parent classes are assigned bar2 B22 = b2.subclassawaredeepcopy (); bar2 B23 = (bar2) f2.subclassawaredeepcopy (); // The parent class returns the subclass instance, which needs to be cast} // compared with 1 else abstract class foo1 <t> {public abstract t subclassawaredeepcopy ();} // because there is no constraint, so we can leave this field at will. You may think that this field does not need to be transformed, // but it only exists in our minds. If it is to provide API interfaces to others, apparently, you must use <t extends Foo <t> class bar1 extends foo1 <foo1> {public bar1 subclassawaredeepcopy () {bar1 B = new bar1 ();//... return B; }}// compare 2 items abstract class foo2 {public abstract foo2 subclassawaredeepcopy ();} class bar2 extends foo2 {public bar2 subclassawaredeepcopy () {bar2 B = new bar2 ();//... return B ;}}}