How to replace run-time instanceof check with compile-time generics validation

前端 未结 6 896
有刺的猬
有刺的猬 2021-01-22 11:52

Got a little puzzle for a true Java Generics specialist... ;)

Let\'s say I have the following two interfaces:

interface Processor {
    void process(Foo          


        
6条回答
  •  有刺的猬
    2021-01-22 12:39

    interface Processor> {
        void process(F fooSubclass);
    }
    
    interface Foo> {
        Processor getProcessor();
    }
    

    I haven't tested that this is precisely right, but this pattern of having the generic type refer to itself is probably about as close as you are going to get with compile-time Java generics.

提交回复
热议问题