Generic Generics:“令牌上的语法错误”扩展“,预期”
public interface View{... public interface Control{... public class RemoteControl<C extends Control> implements Control{...
给我一个“令牌上的语法错误”扩展“,预期”在RemoteControl类的“V extends View”上。
我想以下替代方案是可能的
public class RemoteControl<C extends Control,V extends View> implements Control {...
我仍然想知道这是否不能以更隐式的方式完成,因为后者需要View的冗余声明。 即:
public class TVRemoteControl extends RemoteControl implements TVControl{...
VS
public class TVRemoteControl extends RemoteControl implements TVControl{...
也许我只是被困在一个盒子里,但是有可能以更优雅的方式获得“通用generics”
你建议:
我想以下替代方案是可能的
public class RemoteControl
,V extends View> implements Control {}
这是正确的解决方案,虽然我通常会将其写为(为了便于阅读):
public class RemoteControl> implements Control {}
您正在Control
对象上键入RemoteControl
,该对象也在extends View
的对象上键入。 因此,您需要提供View
的实现,该实现键入键入RemoteControl
对象的Control
对象。
我想你可以解释你的问题,为什么我必须提供V
– 不应该暗示
。 答案是否定的 ,您需要为V
提供一个类型以确保提供正确类型的Control
(即它extends Control
)
如果您不关心键入Control
对象的类型,则无需在RemoteControl
键入Control
:
public class RemoteControl implements Control{}
但是,在V extends View
和RemoteControl implements Control
的事实是RemoteControl implements Control
,而是建议你做…