Java:可能有相互的、最终的类引用吗?

发布于 2024-08-02 16:27:11 字数 248 浏览 9 评论 0原文

假设我有两个类,名为 A 和 B,它们彼此关联,因此如果每个类的对象都包含对另一个类的引用,则最为方便。换句话说,A类有B类的变量“b”。B类有A类的变量“a”。这样,每个类中的代码都可以轻松访问另一个类。

有什么办法可以让这个关联成为“最终”的吗?即A类中的变量b是final的,B类中的变量a是final的?似乎在构造函数中设置这些引用(如final关键字所要求的)需要不合逻辑的循环排序引用。

这更多的是一个概念问题,而不是一个实际问题。谢谢!

Let's say I have two classes, named A and B, that are associated with each other such that it is most convenient if each class's object contains a reference to the other. In other words, class A has a variable "b" of class B. Class B has a variable "a" of class A. This way, the code in each class has easy access to the other class.

Is there any way to set up this association to be "final"? i.e. variable b in class A is final and variable a in class B is final? It seems that setting up these references in a constructor (as would be required by the final keyword) requires an illogical circular sort of reference.

This is more of a conceptual question than a practical one. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小ぇ时光︴ 2024-08-09 16:27:11

是的,如果其中一个类负责创建另一个类的实例,这是可能的。
第一个构造函数可以将其自身的实例作为第二个类的参数传递。

public class A {
    final B b;
    public A() {
        b = new B(this);
    }
    public B getB() {
        return b;
    }
}
public class B {
    final A a;
    public B(A a) {
        this.a = a;
    }
}

Yes it is possible, if one of the class is responsible for creating the instance of the other class.
The first constructor can pass an instance of itself as a parameter for the second class.

public class A {
    final B b;
    public A() {
        b = new B(this);
    }
    public B getB() {
        return b;
    }
}
public class B {
    final A a;
    public B(A a) {
        this.a = a;
    }
}
本王不退位尔等都是臣 2024-08-09 16:27:11

正如另一位发帖者所指出的,可以通过让其中一个类负责构造另一个类并将其自身传递到另一个对象的构造函数中来实现此目的。

但是,如果您希望第一个类的一个实例对应于第二个类的一个实例(看起来您确实这样做),这会导致潜在的问题。另一个对象可能会将对第一个类的现有引用传递到第二个类的构造函数中,从而允许出现无效情况(第二个类的实例现在具有对第一个类的实例的引用,该实例引用了 第二类的不同实例)。

当您认为第二个类的构造函数无法引用第一个类的未初始化成员时,会出现更多潜在问题。

我的建议是,如果这两个类是如此相互关联,以至于您希望它们各自具有对另一个类的最终引用,只需使它们成为同一类。然后你总是得到另一个对象的最终引用:this

As another poster pointed out, it is possible to do this by making one of the classes responsible for constructing the other and passing itself into the constructor for the other object.

However, this lends itself to potential issues if you want one instance of the first class to correspond to one instance of the second class (which it seems you do). Another object could potentially pass an existing reference to the first class into the constructor for the second class, thus allowing an invalid situation (the an instance of the second class now has a reference to an instance of the first class which references a different instance of the second class).

More potential issues arise when you consider that the second class' constructor cannot reference uninitialized members of the first class.

My suggestion is that if the two classes are so interrelated that you want them to each have final references to the other, just make them the same class. Then you've always got a final reference to the other object: this.

菩提树下叶撕阳。 2024-08-09 16:27:11

如果您使用 Di 容器(例如 guice),则无需在构造函数内传递明显的引用即可实现此目的 - 正如前面指出的,这可能容易出错。

声明依赖项 ab 和 ba 并将其中一个注入到另一个类中。 guice 会施展一些魔法,让这两个字段都成为最终的。基本上它会先注入一个代理,然后再设置其委托人。

您需要为此提供代码示例吗?

if you use a Di container such as guice, you can acieve this without obvious reference passing inside the constructor - which may be error-prone as pointed out before.

declare dependency a-b and b-a and inject one of them in another class. guice will do some magic to allow both fields to be final. basically it will inject one proxy first and set its delegator later.

do you need a code sample for this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文