Generic Generics:"令牌上的语法错误"扩展",预期"

Rol*_*and 7 java generics

public interface View{...

public interface Control<V extends View>{...

public class RemoteControl<C extends Control<V extends View>> implements Control<V>{...
Run Code Online (Sandbox Code Playgroud)

给我一个"令牌上的语法错误"扩展",预期"在RemoteControl类的"V extends View"上.

我想以下替代方案是可能的

public class RemoteControl<C extends Control<V>,V extends View> implements Control<V>
{...
Run Code Online (Sandbox Code Playgroud)

我仍然想知道这是否不能以更隐式的方式完成,因为后者需要View的冗余声明.即:

public class TVRemoteControl extends RemoteControl<TVControl,TvView> implements TVControl{...
Run Code Online (Sandbox Code Playgroud)

VS

public class TVRemoteControl extends RemoteControl<TVControl> implements TVControl{...
Run Code Online (Sandbox Code Playgroud)

也许我只是被困在一个盒子里,但是有可能以更优雅的方式获得"泛型泛型"

ama*_*ent 4

您建议:

我想以下替代方案是可能的

public class RemoteControl<C extends Control<V>,V extends View> implements Control<V>{}
Run Code Online (Sandbox Code Playgroud)

这是正确的解决方案,尽管我通常将其写为(为了可读性):

public class RemoteControl<V extends View, C extends Control<V>> implements Control<V>{}
Run Code Online (Sandbox Code Playgroud)

您正在RemoteControl一个Control对象上键入,该对象也正在另一个对象上键入extends View。因此,您需要提供View该类型的对象的实现Control,该对象类型是您的RemoteControl对象的类型。

我想你可以将你的问题解释为,为什么我必须提供V- 不应该从<C extends Control<V>>. 对此,答案是否定的,您需要提供一个类型以V确保提供正确的类型Control(即 it extends Control<V>

如果您不关心对象的类型ViewControl则无需Control输入RemoteControl

public class RemoteControl<C extends Control> implements Control{}
Run Code Online (Sandbox Code Playgroud)

然而,事实Control是在V extends View和上输入的RemoteControl implements Control<V>,而是表明您确实......