为什么初始化子类需要调用父类的相同init函数? [英] why does initializing subclasses require calling the super class's same init function?

查看:245
本文介绍了为什么初始化子类需要调用父类的相同init函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,当您有一个子类时,应该从子类的init中使用相同的init函数来初始化超类.我的意思是子类的init应该调用[super init],子类的initWithFrame应该调用[super initWithFrame].为什么是这样?为什么从子类的initWithFrame调用父级的init会导致无限循环?

I have heard that when you have a subclass, you are supposed to initialize the superclass with the same init function from within the subclass's init. What I mean is that the subclass's init should call [super init] and the subclass's initWithFrame should call [super initWithFrame]. Why is this? Why does calling the super's init from a subclass's initWithFrame result in an infinite loop?

如果这是必需的,那么这是否意味着我无法在诸如initWithPoint之类的子类中创建新的init函数,并且仅仅因为超类没有initWithPoint而具有调用super的init或initWithFrame的功能?我想这里的问题的核心仅仅是为什么为什么不正确地调用另一个超类,这可能由于我的c ++背景而使我感到困惑?

If this is required, then does this mean I can't create a new init function within a subclass such as initWithPoint and have that call super's init or initWithFrame simply because the super class doesn't have initWithPoint? I guess the heart of the question here is simply why it's improper to call a different super class, something that's confusing me possibly because of my c++ background?

推荐答案

创建子类时,如果要实现初始化程序,则必须确保调用超类的指定初始化程序,并且至少必须提供您可以指定一个自己的初始化程序,尽管这可以是您对超类的重写.

When you create a subclass, if you implement an initializer, then you must be sure to call the superclass's designated initializer(s), and you must provide at least one designated initializer of your own, though this can just be your override of the superclass's.

作为初始化子类的一部分,您必须调用超类的指定初始化器之一.

As part of initializing your subclass, you must call one of the superclass's designated initializers.

类的文档应提名其指定的初始化器.如果不是,通常将指定的初始化器假定为超类提供的最具体的初始化器(带有最多参数的初始化器).

A class's documentation should nominate its designated initializers. If not, the designated initializer is generally assumed to be the most specific initializer (the one taking the most arguments) provided by the superclass.

有关更多详细信息,请参见 [注意:自2013年12月起,该内容似乎不再可通过苹果的文档中心.语言参考本已被面向任务的教程和概念性文档所取代.]

For further details, see "The Objective-C Programming Language: Allocating and Initializing Objects." [Note: As of December 2013, this content no longer appears to be available via Apple's doc center. What was a language reference has been replaced by more task-oriented tutorials and conceptual documentation.]

关于您的具体问题:

这是为什么?,以便超类有机会初始化其状态.然后,您可以继续初始化在超类提供的范围之外添加的状态.

Why is this? So that the superclass has a chance to initialize its state. You can then go ahead and initialize the state you add above and beyond what the superclass provides.

为什么从子类的initWithFrame调用父级的init会导致无限循环?因为对于NSView-init不是指定的初始化器,尽管它是NSObject的.因此,NSView会覆盖它以调用其指定的初始化程序-initWithFrame:.如果您已从-initWithFrame:呼叫-init,则现在有-initWithFrame:呼叫-init呼叫-initWithFrame:呼叫-init:呼叫…

Why does calling the super's init from a subclass's initWithFrame result in an infinite loop? Because, for NSView, -init is not the designated initializer, though it is NSObject's. So NSView overrides it to call its designated initializer, -initWithFrame:. If you've called -init from your -initWithFrame:, you now have -initWithFrame: calling -init calling -initWithFrame: calling -init: calling…

是不是……?否,因为这不是必需的.您必须了解实际的文档,而不是传闻.

Does this mean…? No, because this is not required. You must understand the actual documentation, not hearsay.

这篇关于为什么初始化子类需要调用父类的相同init函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆