Java 泛型类型参数隐藏
我正在定义一个类:
class Foo<I extends Bar & Comparable<I>> {
}
编译器抱怨 I
被 I
隐藏。我猜想第二次出现在定义中的 I
隐藏在第一次的作用域中,就好像变量 I
可以分配给两种不同的类型。如何正确做呢?
编辑:
这是一个内部类。完整的代码可以是:
class Baz<I> {
class Foo<I extends Bar & Comparable<I>> {
}
}
现在的问题是,如果我将内部 I
重新指定为 J
,我不确定 I
和J
实际上是相同的类型。
I'm defining a class:
class Foo<I extends Bar & Comparable<I>> {
}
the compiler is complaining about I
being hidden by I
. I guess the second time I
appears in the definition is hiding in scope the first one, as if variable I
could be assigned to two different types. How to do it correctly?
Edit:
this is an inner class. the full code can be:
class Baz<I> {
class Foo<I extends Bar & Comparable<I>> {
}
}
now the problem is that if I re-nominate inner I
to J
, i'm not sure that I
and J
are actually the same types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使内部类参数化:
作为一个内部(非静态嵌套)类,
Baz
声明中定义的I
在Foo 中仍然有意义
,因为每个Foo
都会有一个对其外部Baz
实例的隐式引用。Don't make the inner class parameterized:
As an inner (non-static nested) class,
I
as defined in theBaz
declaration will still have meaning inFoo
, since everyFoo
will have an implicit reference to its outerBaz
instance.如果
I
已在外部类中定义,则只需执行此操作即可。您无法在内部类中重新定义
I
。内部类的I
可能与外部类的I
不同,如果这是您想要的,那么,重命名它。华泰
If
I
is already defined in the outer class just make thisYou cannot redefine
I
in your inner class. TheI
of the inner class would be something else than theI
of the outer class, if this is what you want, well, rename it.HTH