在Java中,我想有如下的东西:
class Clazz<T> {
static void doIt(T object) {
// ...
}
}
但是我知道
Cannot make a static reference to the non-static type T
除了基本用途之外,我不了解泛型,因此无法理解它。我在网上找不到很多关于这个主题的信息,这也无济于事。
有人能澄清一下这种使用是否可能,以类似的方式?还有,为什么我最初的尝试失败了?
在Java中,我想有如下的东西:
class Clazz<T> {
static void doIt(T object) {
// ...
}
}
但是我知道
Cannot make a static reference to the non-static type T
除了基本用途之外,我不了解泛型,因此无法理解它。我在网上找不到很多关于这个主题的信息,这也无济于事。
有人能澄清一下这种使用是否可能,以类似的方式?还有,为什么我最初的尝试失败了?
当前回答
在声明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美元”! !
其他回答
我认为这个语法还没有被提到(在你想要一个没有参数的方法的情况下):
class Clazz {
static <T> T doIt() {
// shake that booty
}
}
还有呼唤:
String str = Clazz.<String>doIt();
希望这能帮助到一些人。
以下内容会让你更接近目标
class Clazz
{
public static <U extends Clazz> void doIt(U thing)
{
}
}
编辑:更新的例子与更多的细节
public abstract class Thingo
{
public static <U extends Thingo> void doIt(U p_thingo)
{
p_thingo.thing();
}
protected abstract void thing();
}
class SubThingoOne extends Thingo
{
@Override
protected void thing()
{
System.out.println("SubThingoOne");
}
}
class SubThingoTwo extends Thingo
{
@Override
protected void thing()
{
System.out.println("SuThingoTwo");
}
}
public class ThingoTest
{
@Test
public void test()
{
Thingo t1 = new SubThingoOne();
Thingo t2 = new SubThingoTwo();
Thingo.doIt(t1);
Thingo.doIt(t2);
// compile error --> Thingo.doIt(new Object());
}
}
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!
错误中正确地提到:您不能对非静态类型T进行静态引用。原因是类型参数T可以被任何类型参数替换,例如Clazz<String>或Clazz<integer>等。但是静态字段/方法由类的所有非静态对象共享。
以下内容摘自纪录片:
A class's static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are not allowed. Consider the following class: public class MobileDevice<T> { private static T os; // ... } If static fields of type parameters were allowed, then the following code would be confused: MobileDevice<Smartphone> phone = new MobileDevice<>(); MobileDevice<Pager> pager = new MobileDevice<>(); MobileDevice<TabletPC> pc = new MobileDevice<>(); Because the static field os is shared by phone, pager, and pc, what is the actual type of os? It cannot be Smartphone, Pager, and TabletPC at the same time. You cannot, therefore, create static fields of type parameters.
正如chris在他的回答中正确指出的那样,在这种情况下,你需要与方法一起使用类型参数,而不是与类一起使用。你可以这样写:
static <E> void doIt(E object)
我也遇到了同样的问题。我通过下载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
}