java中对象传递自身

发布于 2024-11-16 00:47:43 字数 376 浏览 2 评论 0原文

我想出了如何让一个对象将其自身传递给另一个对象并更新其中的字段。 我通过让 ObjectA 将其自身传递给 ObjectB 来实现这一点。然后ObjectB 更改ObjectA 中的字段。 从 Main Method 开始:(并省略方法头等)

ObjA.changeField(Obj2)

In ObjectA

Obj2.callChangeMethod(this);

In ObjectB

Obj1.makeChange();

我困惑的是为什么我必须在 line2 中传递“this”而不是传递 ObjA?

谢谢

I figured out how to have an object pass itself to another and have a field in it updated.
I did so by having ObjectA pass itself to ObjectB. Then ObjectB changes a field in ObjectA.
Starting in Main Method: (and leaving out method headers and such)

ObjA.changeField(Obj2)

In ObjectA

Obj2.callChangeMethod(this);

In ObjectB

Obj1.makeChange();

What I'm confused about is why did I have to pass "this" in line2 versus passing ObjA?

Thanks

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

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

发布评论

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

评论(2

谈场末日恋爱 2024-11-23 00:47:43

其实原因很简单:这都与变量的范围有关。

这是您提供的代码的稍微修饰版本:

public static void main(String[] args) {
    ObjectA Obj1 = new ObjectA();
    ObjectB Obj2 = new ObjectB();

    Obj1.changeField(Obj2);
}

此代码需要注意的是 Obj1Obj2 是在 main 方法。这意味着它们属于 main 方法,不能在 main 之外使用。这就是“范围”的含义。如果变量在类内部声明,则只有该类可以访问它。如果在方法中声明,则只有该方法可以使用它。这同样适用于循环结构以及您可以想象的任何其他类型的块。本质上,如果变量是在一对 {} 内声明的,那么它就属于该对 {}

因此,现在如果您查看 ObjectA 类,您会注意到它是独立存在的 - 它没有声明为 main 的一部分code> 方法,因此它不能使用变量 Obj1 - ObjectA 代码根本不知道 Obj1 存在。

这就是为什么您必须使用 this 关键字。您无权访问 Obj1,因此您需要使用您有权访问的“变量” - 在本例中,您拥有始终引用的 this类的当前实例。

因此,尽管您仍然使用相同的对象(由new ObjectA()创建的对象),但您只是拥有引用该对象的不同变量,具体取决于您使用的代码目前正在查看。作用域规则确实变得有点复杂,但是您使用 Java 的次数越多,对类、实例和实例引用的了解越多,就越容易轻松地使用它们。

The reason is quite simple actually: it all has to do with the scope of the variables.

Here is a slightly embellished version of the code you presented:

public static void main(String[] args) {
    ObjectA Obj1 = new ObjectA();
    ObjectB Obj2 = new ObjectB();

    Obj1.changeField(Obj2);
}

The thing to notice about this code is that Obj1 and Obj2 are declared inside of the main method. This means that they belong to the main method, and cannot be used outside of main. This is what the "scope" means. If a variable is declared inside a class, only that class has access to it. If declared in a method, only that method can use it. The same holds for loop structures, and any other kind of block you can imagine. Essentially, if the variable was declared inside a pair of {}, then it belongs to that pair of {}.

So now if you look at your ObjectA class, you'll notice that it sits all by itself - it wasn't declared as part of the main method, so it can't use the variable Obj1 - the ObjectA code has no idea that Obj1 even exists.

That is why you must use the this keyword. You don't have access to Obj1, so you need to use a "variable" that you do have access to - in this case, you have this which always refers to the current instance of the class.

So although you are still using the same object (the one created by new ObjectA()), you simply have different variables which refer to that object, depending on which code you are currently looking at. The scoping rules do get a little more complex, but the more you play around with Java, and the more you understand classes vs instances vs references to instances, the easier it becomes to use them comfortably.

静水深流 2024-11-23 00:47:43

对象对其自身的唯一引用是 this 关键字。最终,对象没有其他方法来引用自身。

The only reference an object has to itself is the this keyword. Ultimately, there is no other way for an object to refer to itself.

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