在Java中,我们如何知道构造函数中的super()何时完成?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当对
super()
的调用返回时,super()
就完成了(这同样适用于任何其他方法调用)。如果您按照示例显式调用它,则当到达
super()
调用之后的行时,super()
已完成执行。如果您允许隐式调用它,则当执行到达构造函数中的第一行时,super()
已完成。当然,有可能是
super()
生成一个或多个后台线程,这些线程执行各种任务,并且在调用super()
时仍在执行。代码> 返回。但这实际上与对 super() 的调用无关。在这种情况下,其他任务完成的时间与super()
调用完成的时间没有任何关系。super()
is finished when the call tosuper()
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 thesuper()
call is reached. If you are allowing it to be called implicitly, thensuper()
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 tosuper()
returns. But that really has nothing to do with the call tosuper()
. In such a case the time when the other tasks finish has nothing whatsoever to do with the time when the invocation ofsuper()
finishes.在
super()
完成后调用您的代码怎么样:How about calling your code after
super()
finishes like this:简单的答案是“不要这样做”。这是疯狂的。
The simple answer is "don't do this". It is madness.
如果子类实例创建完成,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.
super()
必须是 第一个语句。由于 JVM 将等待第一个语句完成,因此您的第二个语句可以安全地操作对象或根据需要调用
super.methods()
。The
super()
must be the first statement.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.如果你想自动调整列大小,你应该在 dataModel 之后观察。 。数据就在那里。
If you want to do columns auto size you shoud watch after the dataModel . . The data is in there.
您所说的东西称为构造函数链接。
所以是的,您可以在原始子类的构造函数中的 super() 调用之后安全地添加您的方法。
The thing you are talking about is called Constructor Chaining.
So yes, you can safely add your method after the super() call in the constructor of original subclass.