What would be the use of accepting itself as type arguments in generics

后端 未结 2 920
北荒
北荒 2021-02-05 23:30

I saw some code on an unrelated question but it got me curious as I never saw such construct with Java Generics. What would be the use of creating a generic class that can take

2条回答
  •  天涯浪人
    2021-02-06 00:09

    Without this, the parameter to the method foo could not be bound to type E.

    If you have an implementation B of this abstract class, you can now enforce that method foo also requires its parameter to be of type B.

     class B extends A {
          void foo (B x){}
     }
    

    Without that, foo would have to take any kind of A.

    I agree that the syntax for this is less than elegant.

提交回复
热议问题