Java泛型:非静态类型变量T不能从静态上下文中引用 [英] Java Generics: non-static type variable T cannot be referenced from a static context
本文介绍了Java泛型:非静态类型变量T不能从静态上下文中引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
界面A< T> {
interface B {
//结果在非静态类型变量T中不能
//从静态上下文中引用
T foo();
}
}
为什么T从AB引用时看起来是静态的?
解决方案
接口的所有成员字段默认为 public
, static
和 final
。
默认情况下,内部接口默认为 static
,您不能从静态字段引用 T
或方法。
因为 T
实际上与一个类的实例相关联,如果它与一个静态字段或与类关联的方法,那么它就没有任何意义了。
interface A<T> {
interface B {
// Results in non-static type variable T cannot
// be referenced from a static context
T foo();
}
}
Is there anyway round this? Why is T seen as static when referenced from A.B?
解决方案
All member fields of an interface are by default public
, static
and final
.
Since inner interface is static
by default, you can't refer to T
from static fields or methods.
Because T
is actually associated with an instance of a class, if it were associated with a static field or method which is associated with class then it wouldn't make any sense
这篇关于Java泛型:非静态类型变量T不能从静态上下文中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文