RMI:按值传递还是按引用传递?
我很难找到这个问题的明确答案,所以我想我应该在这里用我自己的具体例子来问:
我正在创建一个多人垄断游戏。实际的垄断代码在服务器上运行,而客户端本质上是一个访问和控制该代码的 GUI。垄断游戏由一个名为“银行”的类控制。
假设我在客户端的 main() 中执行了此操作:
Bank banker = server.getBank(); //gets the bank object from server
bank.turn(); //moves the current player
这会在服务器上的 Bank 对象上还是在我的本地计算机上的 Bank 对象的副本上调用 Turn() 吗?
更新:银行未实现远程。它是一个可序列化的对象。
I'm having trouble finding a clear answer to this question so I thought I'd ask here with my own specific example:
I am creating a mulitplayer monopoly game. The actual monopoly code runs on the server and the client is essentially a GUI which accesses and control this code. The monopoly game is controlled by a class called 'Bank'.
Say I did this in the main() of my client:
Bank banker = server.getBank(); //gets the bank object from server
bank.turn(); //moves the current player
Would this call turn() on the Bank object on the server or on a copy of it on my local machine?
Update: Bank does not implement remote. It is a serializable object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于
Bank
是否是Remote
的实例。如果是这样,那么它将通过引用传递(如果所有设置都正确),如果不是,它将被序列化并按值传递。编辑:由于您的
Bank
类不是Remote
,但是Serialized
,那么它将被复制并按值传递。That depends if
Bank
is an instance ofRemote
or not. If so, then it will be passed by reference (if all is set up correctly), if not it'll be serialized and passed by value.edit: Since your
Bank
class is notRemote
, but isSerializable
, then it will be copied and passed by value.这取决于你如何编码。
通常,代表服务器端对象的任何客户端对象都只需进行远程调用并更新服务器端对象。客户端对象只不过是用于调用服务器的传输协议的外观。
如果你使用了RMI,那么它就会遵循这个原则。
That depends on how you coded it.
Typically any client side objects that represent server side objects simply make remote calls with update the server side objects. The client side objects are nothing more than a facade over the transport protocols used to make the calls to the server.
If you used RMI, then it will follow this principle.