Java泛型:不能从静态上下文中引用非静态类型变量T [英] Java Generics: non-static type variable T cannot be referenced from a static context

查看:26
本文介绍了Java泛型:不能从静态上下文中引用非静态类型变量T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface A<T> {

    interface B {
       // Results in non-static type variable T cannot
       // be referenced from a static context
       T foo(); 
    }

}

还有吗?为什么在从 A.B 引用时 T 被视为静态的?

Is there anyway round this? Why is T seen as static when referenced from A.B?

推荐答案

接口的所有成员字段默认为publicstaticfinal.

All member fields of an interface are by default public, static and final.

由于内部接口默认是static,你不能从静态字段或方法中引用T.

Since inner interface is static by default, you can't refer to T from static fields or methods.

因为T实际上是与类的实例相关联的,如果它与与类相关联的静态字段或方法相关联,那么它就没有任何意义

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆