Enforcing return type for an class that implements an interface

后端 未结 7 1225
青春惊慌失措
青春惊慌失措 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:24

    How about this:

    interface Bar
    {
        // everything but getFoo ( ) ;
    }
    
    interface FooGetter < T >
    {
        List < ? extends T > getFoo ( ) ;
    }
    
    interface BarRef < T extends Bar & FooGetter < T > >
    {
        T get ( ) ;
    }
    

    anything that implements BarRef must implement T and anything that implements T must have the appropriately enforced getFoo method.

提交回复
热议问题