在复制构造函数中访问另一个对象的私有字段 - 真的有问题吗?

发布于 2024-08-24 16:44:17 字数 534 浏览 4 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话已封尘 2024-08-31 16:44:17

我看代码没有问题。事实上,我和你一样担心,并且不想使用吸气剂。

您收到的警告到底是什么?也许这与您没有向我们展示的东西有关?

更新: 看来 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.

满意归宿 2024-08-31 16:44:17

这让我想起了ADTObject的讨论。

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, not obj.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.

江湖正好 2024-08-31 16:44:17

我不知道 NetBeans 为何发出警告,但您实际上正在制作副本。您的副本与 src 共享 field1、field2 和 field3,因此对 field3 的修改(例如列表)将反映在原始版本中。

private List field1;

public MyClass(MyClass src) {
    this.field1 = src.field1;
    this.field2 = src.field2;
    this.field3 = src.field3;

    field1.add(new Object()); // field1 of src also modified
...
}

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.

private List field1;

public MyClass(MyClass src) {
    this.field1 = src.field1;
    this.field2 = src.field2;
    this.field3 = src.field3;

    field1.add(new Object()); // field1 of src also modified
...
}
一枫情书 2024-08-31 16:44:17

也许该类可以提供浅复制方法,该方法知道在复制的对象中返回哪些数据。如果需要,您也可以提供深层副本。

public MyClass shallowCopy() {
  MyClass aCopy = new MyClass();
  aCopy.field1 = this.field1;
  aCopy.field2 = this.field2;
  aCopy.field3 = this.field3;
}

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.

public MyClass shallowCopy() {
  MyClass aCopy = new MyClass();
  aCopy.field1 = this.field1;
  aCopy.field2 = this.field2;
  aCopy.field3 = this.field3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文