在复制构造函数中访问另一个对象的私有字段 - 真的有问题吗?
在我的 Java 应用程序中,我有一些像这样的复制构造函数,
public MyClass(MyClass src) {
this.field1 = src.field1;
this.field2 = src.field2;
this.field3 = src.field3;
...
}
现在 Netbeans 6.9 对此发出警告,我想知道这段代码有什么问题?
我的担忧:
- 使用吸气剂可能会带来不必要的副作用。新对象可能不再被视为原始对象的副本。
- 如果建议使用 getter,那么如果对新实例也使用 setter,不是会更加一致吗?
编辑:实际警告是“访问另一个对象的私有字段”,Netbeans 提供的唯一可用操作是添加 @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
我的代码实际上是就像给定的例子一样微不足道。
In my Java application I have some copy-constructors like this
public MyClass(MyClass src) {
this.field1 = src.field1;
this.field2 = src.field2;
this.field3 = src.field3;
...
}
Now Netbeans 6.9 warns about this and I wonder what is wrong with this code?
My concerns:
- Using the getters might introduce unwanted side-effects. The new object might no longer be considered a copy of the original.
- If it is recommended using the getters, wouldn't it be more consistent if one would use setters for the new instance as well?
EDIT: The actual warning is "Access of private field of another object" and the only available action Netbeans offers is adding a @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
My code is actually as trivial as the given example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看代码没有问题。事实上,我和你一样担心,并且不想使用吸气剂。
您收到的警告到底是什么?也许这与您没有向我们展示的东西有关?
更新: 看来 Netbeans 确实在抱怨这一点。我认为,对于随 IDE 一起发布的警告,这是一个相当有争议的警告。
I see no problem with the code. In fact, I share your concerns and would prefer to not use getters.
What exactly is the warning you get? Maybe it is related to something you are not showing us?
Update: Seems Netbeans is really complaining about just that. That is a rather controversial warning to ship with an IDE, I would think.
这让我想起了ADT与Object的讨论。
ADT 可以访问 ADT 的其他实例的内部状态。对象哲学将防止这种情况发生,并通过 getter/setter 强制执行访问,以确保表示独立。
Java 中的实例变量是类私有的,而不是对象私有的,这是经过深思熟虑的选择(在后一种情况下,只有
this.privateInstVar
是允许,而不是obj.privateInstVar
)。这既有优点也有缺点。当涉及到克隆和平等时,它特别方便。另一方面,它可能被滥用并破坏封装。
这几乎是一场哲学辩论。但恕我直言,你所做的很好。
It reminds me of the discussion ADT vs. Object.
ADT can access the internal state of other instance of the ADT. The object philosophy would prevent that and enforce access through getter/setter to ensure representation independence.
It's been a deliberated choice that instances variable in Java are class-private, not object-private (In the later case only
this.privateInstVar
is allowed, notobj.privateInstVar
).This has both strength and weaknesses. It's espcially handy when it comes to cloning and equality. On the other hand it can be misused and break encapsulation.
It's almost a philosophical debate. But IMHO, what you are doing is fine.
我不知道 NetBeans 为何发出警告,但您实际上正在制作浅副本。您的副本与 src 共享 field1、field2 和 field3,因此对 field3 的修改(例如列表)将反映在原始版本中。
I don't know why NetBeans warns, but you are effectively making a shallow copy. Your copy shares field1, field2 and field3 with src and as such a modification of field3, say, a List, will reflect in the original.
也许该类可以提供浅复制方法,该方法知道在复制的对象中返回哪些数据。如果需要,您也可以提供深层副本。
Perhaps the class could provide a shallow copy method which knows which data to return in a copied object. If needed, you could provide a deep-copy too.