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

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

但是我知道

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

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

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


当前回答

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
}

其他回答

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
}

在声明doIt()方法时,可以使用泛型方法的语法来做你想做的事情(注意doIt()方法签名中的static和void之间添加了<T>):

class Clazz<T> {
  static <T> void doIt(T object) {
    // shake that booty
  }
}

我让Eclipse编辑器接受上面的代码,没有Cannot make a static reference to the non-static type T错误,然后将其扩展到以下工作程序(完成一些适合年龄的文化引用):

public class Clazz<T> {
  static <T> void doIt(T object) {
    System.out.println("shake that booty '" + object.getClass().toString()
                       + "' !!!");
  }

  private static class KC {
  }

  private static class SunshineBand {
  }

  public static void main(String args[]) {
    KC kc = new KC();
    SunshineBand sunshineBand = new SunshineBand();
    Clazz.doIt(kc);
    Clazz.doIt(sunshineBand);
  }
}

当我运行它时,它将这些行打印到控制台:

抖抖那个“战利品”类com.eclipseoptions.datamanager。Clazz KC美元”! ! 抖抖那个“战利品”类com.eclipseoptions.datamanager。Clazz SunshineBand美元”! !

@BD at Rivenhill:由于这个老问题在去年重新引起了关注,让我们继续讨论一下,只是为了讨论。 doIt方法的主体根本不做任何t特定的事情。下面就是:

public class Clazz<T> {
  static <T> void doIt(T object) {
    System.out.println("shake that booty '" + object.getClass().toString()
                       + "' !!!");
  }
// ...
}

所以你可以完全放弃所有类型变量,只写代码

public class Clazz {
  static void doIt(Object object) {
    System.out.println("shake that booty '" + object.getClass().toString()
                       + "' !!!");
  }
// ...
}

好的。让我们回到最开始的问题。类声明中的第一个类型变量是多余的。只需要关于方法的第二个步骤。我们又来了,但这还不是最终答案:

public class Clazz  {
  static <T extends Saying> void doIt(T object) {
    System.out.println("shake that booty "+ object.say());
  }

  public static void main(String args[]) {
    Clazz.doIt(new KC());
    Clazz.doIt(new SunshineBand());
  }
}
// Output:
// KC
// Sunshine

interface Saying {
      public String say();
}

class KC implements Saying {
      public String say() {
          return "KC";
      }
}

class SunshineBand implements Saying {
      public String say() {
          return "Sunshine";
      }
}

然而,这太小题大做了,因为下面的版本以同样的方式工作。它所需要的只是方法参数上的接口类型。任何地方都看不到类型变量。这真的是最初的问题吗?

public class Clazz  {
  static void doIt(Saying object) {
    System.out.println("shake that booty "+ object.say());
  }

  public static void main(String args[]) {
    Clazz.doIt(new KC());
    Clazz.doIt(new SunshineBand());
  }
}

interface Saying {
      public String say();
}

class KC implements Saying {
      public String say() {
          return "KC";
      }
}

class SunshineBand implements Saying {
      public String say() {
          return "Sunshine";
      }
}

我也遇到了同样的问题。我通过下载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
}

我认为这个语法还没有被提到(在你想要一个没有参数的方法的情况下):

class Clazz {
  static <T> T doIt() {
    // shake that booty
  }
}

还有呼唤:

String str = Clazz.<String>doIt();

希望这能帮助到一些人。