创建循环通用引用
我正在编写一个应用程序来在对等网络中进行一些分布式计算。在定义网络时,我有两个类:P2PNetwork 和 P2PClient。我希望这些是通用的,因此具有以下定义:
P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>>
P2PClient<T extends P2PNetwork<? extends T>>
使用 P2PClient 定义 setNetwork(T network) 方法。我希望用这段代码描述的是:
- P2PNetwork 是由 某种类型的客户端
- A P2PClient 可能只属于一个 其客户端包括的网络 与此客户端相同的类型( 循环引用)
这对我来说似乎是正确的,但如果我尝试创建非通用版本(例如
MyP2PClient<MyP2PNetwork<? extends MyP2PClient>> myClient;
和其他变体),我会从编译器收到许多错误。所以我的问题如下:
- 通用循环引用是否是 可能(我从未见过任何明确禁止的内容)?
- 上述通用定义是 此类通告的正确定义 关系?
- 如果有效,它是否“正确” 描述这种关系的方式 (即是否还有另一个有效的 定义,这是风格上的 首选)?
- 我将如何正确定义 客户端的非通用实例和 给定正确通用的服务器 定义?
I am writing an application to do some distributed calculations in a peer to peer network. In defining the network I have two class the P2PNetwork and P2PClient. I want these to be generic and so have the definitions of:
P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>>
P2PClient<T extends P2PNetwork<? extends T>>
with P2PClient defining a method of setNetwork(T network). What I am hoping to describe with this code is:
- A P2PNetwork is constituted of
clients of a certain type - A P2PClient may only belong to a
network whose clients consist of the
same type as this client (the
circular-reference)
This seems correct to me but if I try to create a non-generic version such as
MyP2PClient<MyP2PNetwork<? extends MyP2PClient>> myClient;
and other variants I receive numerous errors from the compiler. So my questions are as follows:
- Is a generic circular reference even
possible (I have never seen anything explicitly forbidding it)? - Is the above generic definition a
correct definition of such a circular
relationship? - If it is valid, is it the "correct"
way to describe such a relationship
(i.e. is there another valid
definition, which is stylistically
preferred)? - How would I properly define a
non-generic instance of a Client and
Server given the proper generic
definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
循环通用引用确实是可能的。 Java 泛型和集合 包括几个示例。对于您的情况,这样的样本将如下所示:
Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this:
如果您进一步定义“某种类型”的含义,即P2P网络的各种“类型”之间的差异是什么,可能会帮助我们回答您的问题。
但是,与其用彼此来表达依赖/循环关系,不如通过引入第三个类 P2PType 来表达可能更容易:
我可能会忽略一些东西,但我认为这将允许编译器强制 P2PClient 是相同通用类型的 P2PNetwork 的一部分。
然而,如果“类型”不适合表达为面向对象本身,即如果
P2PType
不是具有方法、多态行为、 ETC。It might help us to answer you if you further defined what "a certain type" means, i.e. what the differences are between various "types" of P2PNetworks.
But instead of expressing the dependency / circular relationship in terms of each other, it might be easier to express by introducing a third class, the
P2PType
:I might be overlooking something but I think this would allow the compiler to enforce that P2PClients are a part of P2PNetworks of the same generic type.
This approach might fall apart however if the "type" isn't something that is suitable to express as a object-oriented itself, i.e. if the
P2PType
isn't something that would have methods, polymorphic behavior, etc.