[英]In Java, how can we know when super() is finished in constructor?
AFAIK: When a subclass is created, all constructors implicitly or explicitly call super(). AFAIK:创建子类时,所有构造函数都隐式或显式调用super()。
Example: 例:
class subclass extends superclass { public subclass() { super(); // unnecessary, but explicit }
How can I know when super() is finished? 我如何知道super()何时完成? I am creating a subclass of JTable that needs to auto-fit columns, but can only do so safely after super() is finished.
我正在创建一个JTable的子类,需要自动匹配列,但只能在super()完成后安全地完成。
-- Edit -- - 编辑 -
To clarify based on comments below, imagine that super() will also call some class methods. 为了澄清下面的评论,想象一下super()也会调用一些类方法。 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...
也许某些必要的成员尚未(完全)初始化......
super()
is finished when the call to super()
returns (the same applies to any other method invocation, as well). 当对
super()
的调用返回时, super()
完成(同样适用于任何其他方法调用)。
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. 如果你按照你的例子显式调用它,那么
super()
已经在紧跟super()
调用之后的行完成了。 If you are allowing it to be called implicitly, then super()
has finished by the time execution reaches the first line in your constructor. 如果允许隐式调用它,那么执行到达构造函数中的第一行时,
super()
已经完成。
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. 当然,可能是这种情况,
super()
产生一个或多个后台线程,这些后台线程执行各种任务,并且在调用super()
返回时仍在执行。 But that really has nothing to do with the call to super()
. 但这与调用
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. 在这种情况下,其他任务完成的时间与调用
super()
完成的时间无关。
How about calling your code after super()
finishes like this: 在
super()
完成之后调用你的代码怎么样:
class subclass extends superclass {
public subclass() {
super(); // unnecessary, but explicit
callYourFunctionHereKnowingThatSuperHasFinished();
}
}
... imagine that super() will also call some class methods.
...想象一下super()也会调用一些类方法。 I may override some of these methods in my subclass
我可能会在我的子类中覆盖其中一些方法
The simple answer is "don't do this". 简单的答案是“不要这样做”。 It is madness.
这很疯狂。
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 - ie you would never have instantiated the subclass, if the super call didn't complete.
如果super()没有完成,它将永远不会无异常地返回来完成子类的构造函数 - 即如果超级调用没有完成,你将永远不会实例化子类。
The super()
must be the first statement . super()
必须是第一个语句 。
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.
因此,每个构造函数中都包含一个隐含的super(),它不包含this()函数或显式的super()调用作为其第一个语句。 The super() statement invokes a constructor of the super class.
super()语句调用超类的构造函数。 The implicit super() can be replaced by an explicit super().
隐式super()可以用显式super()替换。 The super statement must be the first statement of the constructor.
super语句必须是构造函数的第一个语句。
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. 由于JVM将等待第一个语句完成,因此您的第二个语句可以安全地操作对象或调用
super.methods()
。
If you want to do columns auto size you shoud watch after the dataModel . 如果你想做自动调整大小的列,你可以在dataModel之后看。 .
。 The data is in there.
数据在那里。
The thing you are talking about is called Constructor Chaining. 你正在谈论的事情叫做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. 所以,是的,您可以在原始子类的构造函数中的super()调用之后安全地添加您的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.