java中对象传递自身
我想出了如何让一个对象将其自身传递给另一个对象并更新其中的字段。 我通过让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实原因很简单:这都与变量的范围有关。
这是您提供的代码的稍微修饰版本:
此代码需要注意的是
Obj1
和Obj2
是在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:
The thing to notice about this code is that
Obj1
andObj2
are declared inside of themain
method. This means that they belong to the main method, and cannot be used outside ofmain
. 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 themain
method, so it can't use the variableObj1
- theObjectA
code has no idea thatObj1
even exists.That is why you must use the
this
keyword. You don't have access toObj1
, so you need to use a "variable" that you do have access to - in this case, you havethis
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.对象对其自身的唯一引用是
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.