[英]init method in java.util.hashMap
while I was going through the source code of java.util.HashMap
I found declaration of init
. 当我浏览
java.util.HashMap
的源代码时,我发现了init
声明。
Initialization hook for subclasses. 子类的初始化挂钩。 This method is called in all constructors and pseudo-constructors (
clone
, readObject
) after HashMap
has been initialized but before any entries have been inserted. 在初始化
HashMap
之后但在插入任何条目之前,将在所有构造函数和伪构造函数( clone
, readObject
)中调用此方法。 (In the absence of this method, readObject
would require explicit knowledge of subclasses.) (在缺少此方法的情况下,
readObject
将需要明确了解子类。)
void init() {
}
I found a related question In Java, is there a legitimate reason to call a non-final method from a class constructor? 我在Java中发现了一个相关问题,是否有合理的理由从类构造函数中调用非最终方法?
But still I am not able to understand what's the purpose of init and why readObject
require explicit knowledge of subclass? 但是我仍然无法理解init的目的,为什么
readObject
需要显式的子类知识? Could any one explain with an example 任何人都可以举例说明
As the comment says, it's an initialization hook for subclasses. 就像评论所说,这是子类的初始化钩子。 Every subclass can override
init()
appropriately to perform whatever initialization is required in every case, without having to provide another implementation of readObject
for deserialization and reimplement clone()
themselves. 每个子类都可以适当地重写
init()
来执行每种情况下所需的任何初始化, 而不必为反序列化和重新实现clone()
本身提供readObject
另一种实现。
For example, in LinkedHashMap
, the init()
method is overridden to set up the linked list. 例如,在
LinkedHashMap
,将重写init()
方法以建立链接列表。 Think about where that would happen when deserializing if init()
wasn't available. 想想如果
init()
不可用时反序列化时会发生什么情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.