Should __init__() call the parent class's __init__()?

后端 未结 7 1415
-上瘾入骨i
-上瘾入骨i 2020-12-02 07:07

I\'m used that in Objective-C I\'ve got this construct:

- (void)init {
    if (self = [super init]) {
        // init class
    }
    return self;
}
<         


        
7条回答
  •  被撕碎了的回忆
    2020-12-02 07:40

    IMO, you should call it. If your superclass is object, you should not, but in other cases I think it is exceptional not to call it. As already answered by others, it is very convenient if your class doesn't even have to override __init__ itself, for example when it has no (additional) internal state to initialize.

提交回复
热议问题