泛型泛型:“令牌上的语法错误”扩展,“预期” [英] Generic Generics: "Syntax error on token "extends", , expected"
问题描述
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
类型的< RemoteControl
object。
我想你可以解释你的问题,为什么我必须提供 不在乎 然而,事实 gives me a "Syntax error on token "extends", , expected" on "V extends View" for the RemoteControl class. I guess the following alternative was possible 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.: vs 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
This is the correct solution, although I would normally write it as (for readability): You are typing I guess you could interpret your question as saying, why do I have to provide If you don't care what type However, the fact 这篇关于泛型泛型:“令牌上的语法错误”扩展,“预期”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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>{...
public class RemoteControl<C extends Control<V>,V extends View> implements Control<V>
{...
public class TVRemoteControl extends RemoteControl<TVControl,TvView> implements TVControl{...
public class TVRemoteControl extends RemoteControl<TVControl> implements TVControl{...
public class RemoteControl<C extends Control<V>,V extends View> implements Control<V>{}
public class RemoteControl<V extends View, C extends Control<V>> implements Control<V>{}
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.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>
)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{}
Control
is typed on V extends View
and RemoteControl implements Control<V>
, rather suggests that you do...