gpt4 book ai didi

java - 更改会反射(reflect)在原始变量中吗?

转载 作者:行者123 更新时间:2023-12-01 06:44:23 25 4
gpt4 key购买 nike

在下图中,p 指向 person

p 作为对名为 callFunction 的函数的引用传递。现在,如果我对 p 进行任何更改,它们也会反射(reflect)在 person 中吗?请解释一下。

我知道传递的参数是原始变量的引用值(我希望如此!)。但我无法进一步思考。

enter image description here

最佳答案

如果您更改 p 的详细信息,它将得到反射(reflect)

// Nothing done to p before this
p.changeName("Not Sanika Anymore"); // This will be reflected as both point to the same object as references are the same

如果您创建一个新的 Person 并将其分配给 p 那么它不会

// New Person for p
p = new Person("New Sanika"); // now this p is pointing to the newly created object's reference
p.changeName("Not Sanika Anymore"); // This won't be reflected

这是因为 java 纯粹按值传递。您将对象的引用作为值传递给 callFunction()

This answer Eng.Fouad 很好地解释了这个概念。

关于java - 更改会反射(reflect)在原始变量中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com