对象什么时候被创建并可以被引用?

发布于 2024-12-20 15:13:58 字数 1017 浏览 0 评论 0原文

我有一个简单的问题,但在任何地方的参考文献中都找不到解决方案。

当我创建某个类的实例时,每次再次创建该实例时,该实例都会被传播到几个地方,放置代码的最明显的地方是实例本身的创建方法中。它看起来是这样的:

    public void MYTest() {

    public MYTEST() {
         ANOTHER_CLASS.myTest = this; // <-------- can I already use the new this and assign it ?
        }

    }

我可以将这个刚刚创建的实例的赋值给创建方法中已有的其他变量吗?或者我是否必须编写一个额外的“超级”创建方法,如下所示,并将其也放入 MYTEST 中:

public void MYTest() {

    public MYTEST() {
         ANOTHER_CLASS.myTest = this; // <-------- can I already use the new this and assign it ?
        }


    public static MYTEST superCreationMethodForMYTEST() {
        x = new MYTEST();  //      <- here for sure the creation of MYTEST instance is finished BEFORE the assgnment happens
        ANOTHER_ClASS.myTest = x;


        .... all the other assignments for x.....
            }
}

和然后称呼它

x = MYTEST.superCreationMethodForMYTEST();

为:

x = new MYTEST();

谢谢

I have a simple question but can't find the solution in the references anywhere.

When I create an instance of a certain class where the instance is to be propagated to a couple of places everytime it is created again the most obvious place where to put the code is in the creation method of the instance itself. This is how it would look like:

    public void MYTest() {

    public MYTEST() {
         ANOTHER_CLASS.myTest = this; // <-------- can I already use the new this and assign it ?
        }

    }

Can I put the assignments of this just newly created instance to other variables already in the creation method or do I have to write an extra "super" creation method like this and put this also in MYTEST:

public void MYTest() {

    public MYTEST() {
         ANOTHER_CLASS.myTest = this; // <-------- can I already use the new this and assign it ?
        }


    public static MYTEST superCreationMethodForMYTEST() {
        x = new MYTEST();  //      <- here for sure the creation of MYTEST instance is finished BEFORE the assgnment happens
        ANOTHER_ClASS.myTest = x;


        .... all the other assignments for x.....
            }
}

and then call it like

x = MYTEST.superCreationMethodForMYTEST();

instead of:

x = new MYTEST();

Thanks

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

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

发布评论

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

评论(1

不忘初心 2024-12-27 15:13:58

您可以在构造函数中的任何位置分配 this 引用。但是,这可能并不明智,具体取决于您的情况。主要问题是,虽然对象在构造函数时存在,但它尚未完全构造,因此可能无法处于正确处理方法调用的状态。例如,如果在构造函数(和任何子类构造函数)完成之前,被毫无戒心的线程(或构造函数调用的方法)使用分配的引用,则可能会发生不好的事情。

编辑添加:据我了解,您的问题是确保在请求创建对象的人收到新创建的对象之前,将对对象的引用存储在特定位置。一种常见的模式是基本上按照您在问题中猜测的方式进行操作,即使用工厂方法(静态方法)使用 new 来构造对象,然后将其存储在适当的位置。这确保只存储对完全构造的对象的引用。同时,实际的构造函数被声明为privateprotected,以防止在工厂方法之外意外使用new

You can assign the this reference anywhere in the constructor. However, it may not be wise, depending on your situation. The main problem is that while the object exists at the time of the constructor, it is not completely constructed and therefore may not be in a state to handle method calls correctly. If the assigned reference is used by an unsuspecting thread, for example, (or by a method called by your constructor) before your constructor (and any subclass constructors) finish, bad things may happen.

Edited to add: Your problem, as I understand it, is to make sure that a reference to the object is stored in particular places before whoever requested the creation of the object receives the newly created object. One common pattern is to do basically what you surmise in your question, namely having a factory method (a static method) that uses new to construct the object and then stores it in the appropriate places. This ensures that only references to a fully constructed object is stored. At the same time, the actual constructor is declared private or protected to prevent accidental use of new outside the factory method.

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