泛型泛型:“令牌上的语法错误”扩展,“预期” [英] Generic Generics: "Syntax error on token "extends", , expected"

查看:115
本文介绍了泛型泛型:“令牌上的语法错误”扩展,“预期”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public interface View {... 

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

public class RemoteControl< ; C扩展控制< V extends View>>实现Control< V> {...

给我一​​个令牌上的语法错误extends, ,预计在V扩展视图为RemoteControl类。



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

  public class RemoteControl< C extends Control< V>,V extends View>实现Control< V> 
{...

我仍然想知道这不能以更隐含的方式完成因为后者需要视图的冗余声明。 I.e。:

  public class TVRemoteControl扩展了RemoteControl< TVControl,TvView>实现TVControl {... 

vs

  public class TVRemoteControl扩展了RemoteControl< TVControl>实现TVControl {... 

也许我只是再次陷入一个盒子中,但有可能以更优雅的方式获得泛型泛型

解决方案

您建议:


我猜可能有以下替代方案:

  public class RemoteControl< C extends Control< ; V>,V延伸视图>实现Control< V> {} 


这是正确的解决方案,尽管我通常会把它写成(为了便于阅读):

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

您正在输入 RemoteControl 控制对象上,该对象也在扩展视图的对象上键入。因此,您需要提供 View 类型的< Control 对象的实现,该对象键入您的 RemoteControl object。



我想你可以解释你的问题,为什么我必须提供 V - 不应该从< C extends Control< V>> 中暗示。对此,答案是 no ,您需要为 V 提供一个类型,以确保正确类型的 Control 被提供(即它扩展了Control< V>

不在乎 View > Control 对象是什么类型,你不需要输入 Control RemoteControl 中:

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

然而,事实 Control > V extends View > RemoteControl实现Control< V> 时输入, / p>

public interface View{...

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

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

gives me a "Syntax error on token "extends", , expected" on "V extends View" for the RemoteControl class.

I guess the following alternative was possible

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

Still I wonder if this cannot be done in a more implicit way since the latter would require a redundant declaration of the View. I.e.:

public class TVRemoteControl extends RemoteControl<TVControl,TvView> implements TVControl{...

vs

public class TVRemoteControl extends RemoteControl<TVControl> implements TVControl{...

Maybe I'm just stuck in a box again, but is it possible to get the "generic Generics" in a more elegant way

解决方案

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...

这篇关于泛型泛型:“令牌上的语法错误”扩展,“预期”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆