Enforcing return type for an class that implements an interface

后端 未结 7 1224
青春惊慌失措
青春惊慌失措 2021-01-11 11:51

How do I enforce that the method getFoo() in the implementing class, returns a list of the type of same implementing class.

public interface Bar{
     ....
          


        
相关标签:
7条回答
  • 2021-01-11 12:47

    I liked yshavit answer best but I could not understand the code. So I will try to write the code as I think it should be.

    interface Bar
    {
        // stuff
        // don't bother with the getFoo method
    }
    
    interface BarUtils
    {
        < T extends Bar > List < ? extends T > getFoo ( T bar ) ;
    }
    
    0 讨论(0)
提交回复
热议问题