1. Common generic Use
The type that follows <> in the use of the class is the type we determine.
public Class myclass1 <t > { //the generic type defined here is T private T var; public T GetVar () {return var; } public void setVar (T var) {th Is . var = var; }}/** * Most common generics use, only one generic type */ @Test public void testMyClass1 () {myclass1<string> clazz = new Myclass1<string> (); //this thing determines that the generic t corresponding to the object is a string clazz.setvar (); String str = Clazz.getvar (); System.out.println (str);}
As with normal generics, it's just that we can define two different generic types when using classes, and of course we can define multiple as long as our business needs.
Public class MyClass2<K, V> {//define two generic types here PrivateK var1;//The type of the first variable is the specific type of K PrivateV var2;///The type of the second variable is the specific type that v corresponds to PublicK GetVar1 () {returnvar1; } Public voidSetVar1 (K var1) { This. var1 = var1; } PublicV GetVar2 () {returnVAR2; } Public voidSETVAR2 (V var2) { This. var2 = var2; }}/** * contains two generic types of use */@Test Public voidTestMyClass2 () {////Here determines the first generic type of the defined Clazz object is integer, the second generic type is stringMyclass2<integer, string> clazz =NewMyclass2<integer, string> (); CLAZZ.SETVAR1 (1);//This can only be used with parameters of type intCLAZZ.SETVAR2 ("string");//This can only be used with a string type parameterSystem.out.println (CLAZZ.GETVAR1 () +","+ CLAZZ.GETVAR2 ());}
There are also 3 types: Unlimited wildcard characters, using extends restrictions, using super restrictions
Public class MyClass3<T> { PrivateT var; PublicT GetVar () {returnvar } Public voidSetVar (T var) { This. var = var; }@Override PublicString toString () {returnVar.tostring (); }}/** * wildcard characters? The use includes <?>, < Extends type > and <? Super Type > * *@Test Public voidTESTMYCLASS3 () {myclass3<boolean> clazz =NewMyclass3<boolean> (); Clazz.setvar (false); Fun (Clazz);//When dispatching this method, there are no restrictions on generics, and any type of generics can be usedMyclass3<integer> Clazzint =NewMyclass3<integer> (); Clazzint.setvar (1); Funextends (Clazzint);//When dispatching this method, you can only use the number type or its subtypesMyclass3<double> clazzdouble =NewMyclass3<double> (); Clazzdouble.setvar (2.2); Funextends (clazzdouble);//When dispatching this method, you can only use the number type or its subtypesMyclass3<string> clazzstring =NewMyclass3<string> (); Clazzstring.setvar ("string"); Funsuper (clazzstring);//When dispatching this method, you can only use the string type or its parent typeMyclass3<object> Clazzobject =NewMyclass3<object> (); Clazzobject.setvar (NewObject ()); Funsuper (Clazzobject);//When dispatching this method, you can only use the string type or its parent type} Public voidFun (myclass3<?> clazz) {//Unrestricted generics useSystem.out.println (clazz);} Public voidFunextends (myclass3<? extends Number> Clazz) {//can only use generics of number and its subclassesSystem.out.println (clazz);} Public voidFunsuper (myclass3<?SuperString> clazz) {//can only use the generic type of string and its parent classSystem.out.println (clazz);}
4. Restrict generics when defining classes
Public class MyClass4<T extends number > {//define generics for a class when you are restricting generic types PrivateT var; PublicT GetVar () {returnvar } Public voidSetVar (T var) { This. var = var; }@Override PublicString toString () {return This. var.tostring (); }}/** * Defines the generic type of the class when it is given a generic limit * /@Test Public voidTestMyClass4 () {//can also only define generics for number and its subclasses //myclass4<string> clazzstring = new myclass4<string> ();Myclass4<integer> Clazzint =NewMyclass4<integer> (); Myclass4<double> clazzdouble =NewMyclass4<double> (); Myclass4<float> CLAZZFCLASS4 = Fun (1.1f);the parameter to be dispatched here is the float type, which determines that the return type must be a float} Public<t extends number> myclass4<t> fun (T Arg) {return NewMyclass4<t> ();}
Public interface myinterface<T> { PublicT GetVar ();}//two ways to implement. 1, when the implementation of the use of generics, to the specific definition of the object and then determine Public class Myinterface1impl<t> implements MyInterface<t > { PrivateT var; PublicMyinterface1impl () {} PublicMyinterface1impl (T var) { This. var = var; }@Override PublicT GetVar () {return This. var; }}///second implementation, which determines the type of generics at the time of implementation Public class Myinterface2impl implements MyInterface<String> { PrivateString Varstr; PublicMyinterface2impl () {} PublicMyinterface2impl (String varstr) { This. varstr = Varstr; }@Override PublicString GetVar () {return This. varstr; }}/** * Use of generic interfaces * /@Test Public voidTestmyinterface () {The//implementation class can be defined as any type of genericMyinterface1impl<string> Varstr =NewMyinterface1impl<string> ("ABC"); System.out.println (Varstr.getvar ()); Myinterface1impl<integer> Varint =NewMyinterface1impl<integer> (123); System.out.println (Varint.getvar ());//has been identified only as a string at the time of the class implementationMyinterface2impl var =NewMyinterface2impl ("CBA"); String str = Var.getvar (); System.out.println (str);}
Public class MyFunction { Public<T> t fun1 (t Arg) {//Incoming parameters and return parameters are the same generic type returnArg } Public<T>voidFun2 (T Arg) {//incoming parameter is generic and does not need to be returned if(ARGinstanceofString) {System.out.println ("T is StringType"); }Else if(ARGinstanceofInteger) {System.out.println ("T is Integertype"); }Else{System.out.println ("T is Othertype"); } } Public<T> String fun3 (T Arg) {The parameter passed in is generic, the determined type returned returnArg.tostring (); }}/** * Use of generic methods * /@Test Public voidMyFunction () {MyFunction clazz =NewMyFunction ();//What type to pass in, and what type to returnString var1 = clazz.fun1 ("ABC");intVAR2 = CLAZZ.FUN1 ( A); System.out.println (VAR1); System.out.println (VAR2);//Regardless of the type of incoming, it's okayClazz.fun2 (1); Clazz.fun2 (false); Clazz.fun2 ("string");//No matter what is passed in, the string is returnedString Var3 = Clazz.fun3 (123); String VAR4 = Clazz.fun3 ("string"); System.out.println (VAR3); System.out.println (VAR4);}
/** * Generic array use */ @Test span class= "keyword" >public void testarray () {integer[] arr = Fun (1 , 2 , 3 ); }public <T> t[] Fun (t ... args) {//what type to pass in, T is what type, And you can use generics to traverse the for (T T:args) {System.out.println (t.tostring ()); } return args;}
/** * Nested generics * / @Test Public The type of void testnest () { //outer-layer generics is essentially an inner generic, and when the inner-layer generics are determined, the outer generics also determine the new myclass1< Myclass2<integer,string>> (); New Myclass2<integer,string> (); Nestin.setvar1 (1); Nestin.setvar2 ("a"); Nestout.setvar (nestIn); System.out.println (Nestout.getvar (). GETVAR1 ()); System.out.println (Nestout.getvar (). GETVAR2 ());}
The use of generics in Java