Java generics type parameter hiding

后端 未结 2 1194
北海茫月
北海茫月 2021-01-06 02:21

I\'m defining a class:

class Foo> {
}

the compiler is complaining about I being

相关标签:
2条回答
  • 2021-01-06 02:42

    If I is already defined in the outer class just make this

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

    You cannot redefine I in your inner class. 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.

    HTH

    0 讨论(0)
  • 2021-01-06 02:50

    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.

    0 讨论(0)
提交回复
热议问题