从 Java 客户端程序传递远程对象的构造函数参数
我对 RMI 有疑问。 在 RMI 中,我们创建远程对象并使用它们来调用远程接口中声明的方法。因此,如果我想设置远程对象的某些参数,我必须使用该对象的某些 setField 方法(如创建的那样)来执行此操作。
然而,由于初始化对象字段的最常见方法是通过类的构造函数,我不能从客户端传递参数,以便在创建远程对象时(通过服务器中的 new )使用这些参数吗?
I have a doubt regarding RMI.
In RMI we create remote object(s) and use them to call methods as declared in the remote interface. So if I want to set some parameters of a remote object I have got to do so using some setField method ( as created ) for the object.
However since the most common way of initializing fields of an object is by the constructor of the class, can't I pass arguments from my client such that while the remote object is being created ( by new in the server ) these shall be utilized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是什么让您认为将参数传递给构造函数是初始化字段的最常见方法?我宁愿说,所有字段都是在对象创建期间初始化的,但只有少数字段是通过构造函数参数初始化的。
是的,字段在创建对象时初始化。实例上没有未定义的字段值。但这很常见,实例字段在创建实例之后才接收其实际值。创建一些默认实例并稍后通过 setter 方法甚至使用依赖项注入来设置所需的值是很常见的。
What makes you think, that passing arguments to a constructor is the most common way to initialize fields? I'd rather say, that all fields are initialized during creation of the object but only a few through constructor arguments.
Yes, fields are initialized while the object is created. No field value is undefined on an instance. But it's pretty common, that instance fields receive their actual values after the instance has been created. It's pretty common to create some default instance and set the required values later through setter methods or even with dependency injection.
你的问题没有意义。远程对象必须在服务器 JVM 上构建并导出,然后客户端才能使用它执行任何操作。
Your question doesn't make sense. The remote object has to be constructed at the server JVM and exported before the client can do anything with it.