对象实例重用怎么样?
我想知道Java中是否有任何约定(通过Javadoc或隐式约定)来指示给定的相同类实例可以在多个上下文中重用?
我这么问是因为在 SWT 布局上下文中,我不知道是否允许为多个 Composite
对象重用相同的 GridLayout
对象。我想在检查源代码后答案是肯定的(我看不到任何状态字段),但 Javadoc 没有明确说明。也许隐含的约定是,如果没有明确禁止,则允许“共享”?
I am wondering if there is any convention in Java (through Javadoc or implicit convention) to indicate that a given same class instance can be reuse in several contexts ?
I am asking that because in the SWT layout context I don't know if I am allowed to reuse the same GridLayout
object for several Composite
objects. I guess the answer is Yes after checking the source code (I can't see any state fields) but the Javadoc doesn't explicitly state it. Maybe the implicit convention is that "sharing" is allowed if not explicitly forbidden ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有这样的约定。
相反,你应该采取谨慎的态度;也就是说,只有当 Javadocs 明确指出您可以安全地执行此操作时,您才应该重用实例。
阅读代码并观察到与当前实现共享是安全的,并不能保证在未来版本或早期版本中共享也是安全的。仅依赖记录的行为是一个好主意。
There is no such convention.
Instead, you should take the prudent approach; i.e. you should only reuse instances if the Javadocs explicitly states that you can do this safely.
Reading the code and observing that it is safe to share with the current implementation is no guarantee that it will be safe to share in future releases, or in earlier releases. It is a good idea to only rely on behavior that is documented.