Java generic method definition

One o'clock eye

1 If you define a class, interface type parameter is not used, but would like to define methods that define when a type parameter, it is also possible, JDK1.5 also provides support for a generic method.

2 generic method syntax is as follows:

Modifier <T, S> return type method name (parameter list)

{

    // method body ...

}

Method 3 generic method signature than the signature method of the conventional method type parameter declaration, the type of parameter in angle brackets declaration, a comma (,) between a plurality of spaced type parameters, all type parameters declared modifier on methods and methods for returning a value type.

And class 4, the generic interface using different parameters, the method of the generic parameters without explicitly passed in the actual type arguments, because the compiler in accordance with a value type parameter argument estimation. It is usually the most straightforward to infer the type of parameter.

Five times can be used instead of the generic type of a wildcard method.

6 allows generic method type parameter is used to indicate the type of dependency between one or more parameters of a method, or a method return type dependency relationship between the value of the parameters. If there is no such type of dependency, we should not use the generic method.

Two combat

import java.util.*;

public class GenericMethodTest
{
   // 声明一个泛型方法,该泛型方法中带一个T类型形参,
   static <T> void fromArrayToCollection(T[] a, Collection<T> c)
   {
      for (T o : a)
      {
         c.add(o);
      }
   }
   public static void main(String[] args)
   {
      Object[] oa = new Object[100];
      Collection<Object> co = new ArrayList<>();
      // 下面代码中T代表Object类型
      fromArrayToCollection(oa, co);
      String[] sa = new String[100];
      Collection<String> cs = new ArrayList<>();
      // 下面代码中T代表String类型
      fromArrayToCollection(sa, cs);
      // 下面代码中T代表Object类型
      fromArrayToCollection(sa, co);
      Integer[] ia = new Integer[100];
      Float[] fa = new Float[100];
      Number[] na = new Number[100];
      Collection<Number> cn = new ArrayList<>();
      // 下面代码中T代表Number类型
      fromArrayToCollection(ia, cn);
      // 下面代码中T代表Number类型
      fromArrayToCollection(fa, cn);
      // 下面代码中T代表Number类型
      fromArrayToCollection(na, cn);
      // 下面代码中T代表Object类型
      fromArrayToCollection(na, co);
      // 下面代码中T代表String类型,但na是一个Number数组,
      // 因为Number既不是String类型,
      // 也不是它的子类,所以出现编译错误
//    fromArrayToCollection(na, cs);
   }
}

Mistake usage

import java.util.*;

public class ErrorTest
{
   // 声明一个泛型方法,该泛型方法中带一个T类型形参,两个参数类型必须一致
   static <T> void test(Collection<T> from, Collection<T> to)
   {
      for (T ele : from)
      {
         to.add(ele);
      }
   }
   public static void main(String[] args)
   {
      List<Object> as = new ArrayList<>();
      List<String> ao = new ArrayList<>();
      // 下面代码将产生编译错误,传参类型不一致,编译器迷惑了
      // test(as , ao);
   }
}

Four correct usage

import java.util.*;

public class RightTest
{
   // 声明一个泛型方法,该泛型方法中带一个T形参
   static <T> void test(Collection<? extends T> from , Collection<T> to)
   {
      for (T ele : from)
      {
         to.add(ele);
      }
   }
   public static void main(String[] args)
   {
      List<Object> ao = new ArrayList<>();
      List<String> as = new ArrayList<>();
      // 下面代码完全正常,因为前一个集合的元素类型是后一个集合元素类型的子类型
      test(as , ao);
   }
}

 

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/94767664