在 Java 中将自引用传递给新对象
是否可以实例化一个对象并将对该新对象本身的引用作为参数之一传递,而无需修改类中的代码? 示例:
SuperObject obj = new SuperObject(<"pointer to this new SuperObject">);
谢谢!
编辑:强调:我不想修改类内的代码。这个运算符没有帮助!
Is it possible to instantiate an object and pass a reference to that new object itself as one of the parameters without modifying the code in the class?
Example:
SuperObject obj = new SuperObject(<"pointer to this new SuperObject">);
Thanks!
edit:EMPHASIS: I do not want to modify the code inside the class. The this operator does not help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么?该对象已经有了“this”:它不需要任何其他东西。
Why? The object already has 'this': it doesn't need anything else.
不,你不能。如果要将对象设置为其成员之一,则应该寻找另一种允许设置该成员的方法,并将对象传递给该方法:
No, you cant. If you want to set the object as one of its members, you should look for another method that allows you to set that member, and pass the object in to that method:
在
SuperObject
内部,您可以使用关键字this
直接引用自身。您不需要将其传递到构造函数内..Inside
SuperObject
you can directly reference itself by using the keywordthis
. You don't need to pass it inside constructor..