Java泛型中在方法/构造函数之前使用类型参数

Java泛型中在方法/构造函数之前使用类型参数,java,generics,Java,Generics,我正在学习Java中的泛型,在谈到创建泛型方法这一主题之前,我一直过得很愉快 我知道,在Java中,当您想要实现某个东西时,不管程序或方法操作的数据类型是什么,都会使用泛型。因此,您可以将一个泛型类作为类Gen,然后在一个非泛型类GenDemo中包含main。然后,您可以为不同的数据类型(如Gen iOB和Gen strOB)创建Gen引用 但是,在创建泛型方法的示例中,本书给出了以下代码: //This is a simple generic method class GenMethDemo

我正在学习Java中的泛型,在谈到创建泛型方法这一主题之前,我一直过得很愉快

我知道,在Java中,当您想要实现某个东西时,不管程序或方法操作的数据类型是什么,都会使用泛型。因此,您可以将一个泛型类作为类Gen,然后在一个非泛型类GenDemo中包含main。然后,您可以为不同的数据类型(如Gen iOB和Gen strOB)创建Gen引用

但是,在创建泛型方法的示例中,本书给出了以下代码:

//This is a simple generic method

class GenMethDemo
{

   //determine if an object is in an array
   static<T,V extends T> boolean isIn(T x, V[] y)
   {

      for (int i=0; i<y.length; i++)
          if(x.equals(y[i])) 
            return true;
          else
            return false;
   }

public static void main(String[] args)
  {
   //use isIn() on Integers
   Integer nums[]={1,2,3,4,5};

   if(isIn(2,nums))
   System.out.println("2 is in nums");

   if(!isIn(7,nums))
   System.out.println("2 is in nums");

   //use isIn() on Strings
   String strs[]={"one", "two", "three", "four", "five"};

   if(!(isIn("two", strs))
   System.out.println("two is in strs");

  }
}
和以前一样,我被这句话难住了:GenConsT arg。为什么在声明构造函数之前使用?也可以这样写:GenCons arg

非常感谢您的帮助。

请注意,在GenMethDemo和GenCons类中,类本身没有泛型类型。它不是类GenMethDemo,而是类GenMethDemo

所以,如果GenMethDemo和GenCons不是泛型类,那么如何使用泛型呢?这似乎是一个矛盾


Java还允许您定义泛型方法。如果我使用静态布尔值isInT x,V[]y,就好像我实际使用了类GenMethDemo,只是类型变量T和V的作用域仅限于该特定方法。当您不一定希望整个类使用泛型时,这会很有用;只有一两种方法是真正需要的。

你能告诉我从哪里得到创建泛型构造函数的示例吗?
static <T,V extends T> boolean isIn(T x, V[] y)
class GenCons
    {
       private double val;

       <T extends Number> GenCons(T arg)
          {
             val=arg.doubleValue();
          }

        void showVal()
          {
            System.out.println("Val: "+ val); 
          }

    }