如何应对无法通过引用传递?
我发现理解不同对象如何通信和交换信息非常困难。
来自 C/C++ 世界,当我需要将对象提供给类/函数进行处理时,我习惯于通过引用传递对象。
我确信有一种众所周知的模式可以实现干净且可维护的对象通信方式。我只需要找出它是什么。
编辑:示例
ObjectThatNeedsProcessing obj;
WizardDialog dialog = new WizardDialog;
dialog.addObjectToBeProcessed(obj);
dialog.show();
//When the dialog is finished obj would be changed.
致以诚挚的问候
I'm finding it incredibly difficult to comprehend how different objects should communicate and exchange information.
Coming from the C/C++ world, I'm used to pass objects by reference when I need to give an object to a class/function for processing.
I'm certain that there's a we'll known pattern for achieving clean and maintainable way for object communication. I just need to find out what it is.
EDIT: Example
ObjectThatNeedsProcessing obj;
WizardDialog dialog = new WizardDialog;
dialog.addObjectToBeProcessed(obj);
dialog.show();
//When the dialog is finished obj would be changed.
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将一个对象传递给 java 中的方法时,将对现有对象进行另一个引用。
所以你有两个对同一个对象的引用。
When you pass an object to a method in java, another reference is made to the existing object.
So you have 2 references to the same object.
我不是 C++ 程序员,但在 C 中,你可以按值传递所有内容(即使指针也是按值传递,它们只是指向某个东西) - 这在 Java 中也是一样的。
对于下一个将此标记为负面的人 - 请解释原因,以便其他人理解。
I'm no C++ programmer but in C you pass everything by value (even pointers are passed by value, they just point to something) - and it's the same in Java.
And for the next person who marks this as negative - please explain why so that everyone else understands.