在Java中,我想有如下的东西:

class Clazz<T> {
  static void doIt(T object) {
    // ...
  }
}

但是我知道

Cannot make a static reference to the non-static type T

除了基本用途之外,我不了解泛型,因此无法理解它。我在网上找不到很多关于这个主题的信息,这也无济于事。

有人能澄清一下这种使用是否可能,以类似的方式?还有,为什么我最初的尝试失败了?


当前回答

我也遇到了同样的问题。我通过下载Collections的源代码找到了答案。在Java框架中进行排序。我使用的答案是在方法中放入<T>泛型,而不是在类定义中。

所以这是可行的:

public class QuickSortArray  {
    public static <T extends Comparable> void quickSort(T[] array, int bottom, int top){
//do it
}

}

当然,在阅读了上面的答案之后,我意识到这将是一个不使用泛型类的可接受的替代方案:

public static void quickSort(Comparable[] array, int bottom, int top){
//do it
}

其他回答

我也遇到了同样的问题。我通过下载Collections的源代码找到了答案。在Java框架中进行排序。我使用的答案是在方法中放入<T>泛型,而不是在类定义中。

所以这是可行的:

public class QuickSortArray  {
    public static <T extends Comparable> void quickSort(T[] array, int bottom, int top){
//do it
}

}

当然,在阅读了上面的答案之后,我意识到这将是一个不使用泛型类的可接受的替代方案:

public static void quickSort(Comparable[] array, int bottom, int top){
//do it
}

T不在静态方法的范围内,因此不能在静态方法中使用T。您需要为静态方法定义一个不同的类型参数。我会这样写:

class Clazz<T> {

  static <U> void doIt(U object) {
    // ...
  }

}

例如:

public class Tuple<T> {

    private T[] elements;

    public static <E> Tuple<E> of(E ...args){
        if (args.length == 0) 
             return new Tuple<E>();
        return new Tuple<E>(args);
    }

    //other methods
}

当您为类指定泛型类型时,JVM只知道它是类的实例,而不是定义。每个定义只有参数化类型。

泛型的工作方式类似于c++中的模板,因此您应该首先实例化类,然后使用指定类型的函数。

Also to put it in simple terms, it happens because of the "Erasure" property of the generics.Which means that although we define ArrayList<Integer> and ArrayList<String> , at the compile time it stays as two different concrete types but at the runtime the JVM erases generic types and creates only one ArrayList class instead of two classes. So when we define a static type method or anything for a generic, it is shared by all instances of that generic, in my example it is shared by both ArrayList<Integer> and ArrayList<String> .That's why you get the error.A Generic Type Parameter of a Class Is Not Allowed in a Static Context!

直到实例化一个类型,Java才知道T是什么。

也许你可以通过调用Clazz<T>.doit(一些东西)来执行静态方法,但听起来你不能。

另一种处理方法是将类型参数放在方法本身中:

static <U> void doIt(U object)

这并不能让你对U有正确的限制,但总比没有好....