在Java中,我们如何知道构造函数中的super()何时完成?

发布于 2024-12-11 19:06:45 字数 384 浏览 0 评论 0原文

AFAIK:创建子类时,所有构造函数都会隐式或显式调用 super()。

示例:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
    }

我如何知道 super() 何时完成?我正在创建一个需要自动调整列的 JTable 子类,但只能在 super() 完成后安全地执行此操作。

-- 编辑 --

为了根据下面的注释进行澄清,假设 super() 还将调用一些类方法。我可以在我的子类中重写其中一些方法。我想在基类构造期间修改这些方法中的行为。也许某些必需的成员尚未(完全)初始化......

AFAIK: When a subclass is created, all constructors implicitly or explicitly call super().

Example:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
    }

How can I know when super() is finished? I am creating a subclass of JTable that needs to auto-fit columns, but can only do so safely after super() is finished.

-- Edit --

To clarify based on comments below, imagine that super() will also call some class methods. I may override some of these methods in my subclass. I want to modify behaviour in these methods during base class construction. Perhaps certain required members will not yet be (completely) initialised...

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

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

发布评论

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

评论(7

吃不饱 2024-12-18 19:06:45

当对 super() 的调用返回时,super() 就完成了(这同样适用于任何其他方法调用)。

如果您按照示例显式调用它,则当到达 super() 调用之后的行时,super() 已完成执行。如果您允许隐式调用它,则当执行到达构造函数中的第一行时,super() 已完成。

当然,有可能是 super() 生成一个或多个后台线程,这些线程执行各种任务,并且在调用 super() 时仍在执行。代码> 返回。但这实际上与对 super() 的调用无关。在这种情况下,其他任务完成的时间与 super() 调用完成的时间没有任何关系。

super() is finished when the call to super() returns (the same applies to any other method invocation, as well).

If you are calling it explicitly as per your example, then super() has finished executing when the line immediately after the super() call is reached. If you are allowing it to be called implicitly, then super() has finished by the time execution reaches the first line in your constructor.

Of course, it may be that case that super() is spawning one or more background threads which perform various tasks and which are still executing at the time when the call to super() returns. But that really has nothing to do with the call to super(). In such a case the time when the other tasks finish has nothing whatsoever to do with the time when the invocation of super() finishes.

请叫√我孤独 2024-12-18 19:06:45

super() 完成后调用您的代码怎么样:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
        callYourFunctionHereKnowingThatSuperHasFinished();
    }
}

How about calling your code after super() finishes like this:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
        callYourFunctionHereKnowingThatSuperHasFinished();
    }
}
后eg是否自 2024-12-18 19:06:45

...想象一下 super() 也会调用一些类方法。我可以覆盖
我的子类中的一些方法

简单的答案是“不要这样做”。这是疯狂的。

... imagine that super() will also call some class methods. I may override
some of these methods in my subclass

The simple answer is "don't do this". It is madness.

长伴 2024-12-18 19:06:45

如果子类实例创建完成,Super 就完成了。如果 super() 没有完成,它永远不会无异常地返回来完成子类的构造函数 - 即,如果 super 调用没有完成,你将永远不会实例化子类。

Super is finished if the subclass instance was created. If the super() didn't complete, it would never return without exception to finish the constructor of the subclass - i.e. you would never have instantiated the subclass, if the super call didn't complete.

<逆流佳人身旁 2024-12-18 19:06:45

super() 必须是 第一个语句

因此,每个构造函数中都包含隐含的 super()
不包含 this() 函数或显式 super()
调用作为它的第一个语句。 super() 语句调用
超类的构造函数。隐式的 super() 可以被替换
通过显式的 super()。 super 语句必须是第一个
构造函数的声明。

由于 JVM 将等待第一个语句完成,因此您的第二个语句可以安全地操作对象或根据需要调用 super.methods()

The super() must be the first statement.

An implied super() is therefore included in each constructor which
does not include either the this() function or an explicit super()
call as its first statement. The super() statement invokes a
constructor of the super class. The implicit super() can be replaced
by an explicit super(). The super statement must be the first
statement of the constructor.

And since the JVM will wait for the first statement to finish, your second statement is safe to manipulate the object or call super.methods() as it pleases.

北城半夏 2024-12-18 19:06:45

如果你想自动调整列大小,你应该在 dataModel 之后观察。 。数据就在那里。

If you want to do columns auto size you shoud watch after the dataModel . . The data is in there.

携君以终年 2024-12-18 19:06:45

您所说的东西称为构造函数链接。

每个构造函数方法都会调用链,直到到达并初始化顶部的类。然后,当链返回到原始子类时,下面的每个后续类都会被初始化。这个过程称为构造函数链。

所以是的,您可以在原始子类的构造函数中的 super() 调用之后安全地添加您的方法。

The thing you are talking about is called Constructor Chaining.

Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.

So yes, you can safely add your method after the super() call in the constructor of original subclass.

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