简体   繁体   English

Java泛型类型参数隐藏

[英]Java generics type parameter hiding

I'm defining a class: 我正在定义一个类:

class Foo<I extends Bar & Comparable<I>> {
}

the compiler is complaining about I being hidden by I . 编译器抱怨I被隐藏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. 我想在第二时间I出现在定义的范围的第一个隐藏,仿佛可变I可以分配给两个不同的类型。 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. 现在的问题是,如果我重新提名内部IJ ,我不确定IJ实际上是相同的类型。

Don't make the inner class parameterized: 不要使内部类参数化:

class Baz<I extends Bar & Comparable<I>> {
   class Foo {
   }
}

As an inner (non-static nested) class, I as defined in the Baz declaration will still have meaning in Foo , since every Foo will have an implicit reference to its outer Baz instance. 作为一个内部(非静态嵌套)类, IBaz声明中定义的仍然具有Foo意义,因为每个Foo都有一个对其外部Baz实例的隐式引用。

If I is already defined in the outer class just make this 如果I已经在外部类中定义了就这样做了

public class Outer<I extends Bar & Comparable<I>> {
  public class Foo<I> {
  } 
}

You cannot redefine I in your inner class. 不能在你的内心阶级重新定义I The I of the inner class would be something else than the I of the outer class, if this is what you want, well, rename it. 内部类的I将不是外部类的I ,如果这是你想要的,那么,重命名它。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM