在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
{
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());
}
}
因为静态变量由类的所有实例共享。例如,如果你有以下代码
class Class<T> {
static void doIt(T object) {
// using T here
}
}
T仅在创建实例后可用。但是静态方法甚至可以在实例可用之前使用。因此,泛型类型参数不能在静态方法和变量中引用
当您为类指定泛型类型时,JVM只知道它是类的实例,而不是定义。每个定义只有参数化类型。
泛型的工作方式类似于c++中的模板,因此您应该首先实例化类,然后使用指定类型的函数。
在声明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美元”! !
直到实例化一个类型,Java才知道T是什么。
也许你可以通过调用Clazz<T>.doit(一些东西)来执行静态方法,但听起来你不能。
另一种处理方法是将类型参数放在方法本身中:
static <U> void doIt(U object)
这并不能让你对U有正确的限制,但总比没有好....