Generic Generics: “Syntax error on token ”extends“, , expected”

前端 未结 1 2003
谎友^
谎友^ 2020-12-20 18:24
public interface View{...

public interface Control{...

public class RemoteControl> implements C         


        
相关标签:
1条回答
  • 2020-12-20 18:48

    You suggest:

    I guess the following alternative was possible

    public class RemoteControl<C extends Control<V>,V extends View> implements Control<V>{}
    

    This is the correct solution, although I would normally write it as (for readability):

    public class RemoteControl<V extends View, C extends Control<V>> implements Control<V>{}
    

    You are typing RemoteControl on a Control object which is also typed on an object that extends View. As such you need to provide the implementation of View that types the Control object that types your RemoteControl object.

    I guess you could interpret your question as saying, why do I have to provide V - shouldn't it be implied from the <C extends Control<V>>. To which, the answer is no, you need to provide a type for V to ensure that the right type of Control is provided (i.e. that it extends Control<V>)

    If you don't care what type View the Control object is typed on, you don't need to type Control in the RemoteControl:

    public class RemoteControl<C extends Control> implements Control{}
    

    However, the fact Control is typed on V extends View and RemoteControl implements Control<V>, rather suggests that you do...

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