1. <tfoot id='I6AFJ'></tfoot>

      • <bdo id='I6AFJ'></bdo><ul id='I6AFJ'></ul>
    1. <i id='I6AFJ'><tr id='I6AFJ'><dt id='I6AFJ'><q id='I6AFJ'><span id='I6AFJ'><b id='I6AFJ'><form id='I6AFJ'><ins id='I6AFJ'></ins><ul id='I6AFJ'></ul><sub id='I6AFJ'></sub></form><legend id='I6AFJ'></legend><bdo id='I6AFJ'><pre id='I6AFJ'><center id='I6AFJ'></center></pre></bdo></b><th id='I6AFJ'></th></span></q></dt></tr></i><div id='I6AFJ'><tfoot id='I6AFJ'></tfoot><dl id='I6AFJ'><fieldset id='I6AFJ'></fieldset></dl></div>

      <small id='I6AFJ'></small><noframes id='I6AFJ'>

      <legend id='I6AFJ'><style id='I6AFJ'><dir id='I6AFJ'><q id='I6AFJ'></q></dir></style></legend>

      为什么初始化子类需要调用超类的相同init函数?

      why does initializing subclasses require calling the super class#39;s same init function?(为什么初始化子类需要调用超类的相同init函数?)
        <bdo id='ffXEo'></bdo><ul id='ffXEo'></ul>

          <tbody id='ffXEo'></tbody>
        <i id='ffXEo'><tr id='ffXEo'><dt id='ffXEo'><q id='ffXEo'><span id='ffXEo'><b id='ffXEo'><form id='ffXEo'><ins id='ffXEo'></ins><ul id='ffXEo'></ul><sub id='ffXEo'></sub></form><legend id='ffXEo'></legend><bdo id='ffXEo'><pre id='ffXEo'><center id='ffXEo'></center></pre></bdo></b><th id='ffXEo'></th></span></q></dt></tr></i><div id='ffXEo'><tfoot id='ffXEo'></tfoot><dl id='ffXEo'><fieldset id='ffXEo'></fieldset></dl></div>
        <tfoot id='ffXEo'></tfoot>
            1. <small id='ffXEo'></small><noframes id='ffXEo'>

                <legend id='ffXEo'><style id='ffXEo'><dir id='ffXEo'><q id='ffXEo'></q></dir></style></legend>
              1. 本文介绍了为什么初始化子类需要调用超类的相同init函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我听说当你有一个子类时,你应该在子类的 init 中使用相同的 init 函数来初始化超类.我的意思是子类的init应该调用[super init],而子类的initWithFrame应该调用[super initWithFrame].为什么是这样?为什么子类的initWithFrame调用super的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.

                有关详细信息,请参阅 Objective-C 编程语言:分配和初始化对象." [注意:截至 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调用super的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函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                How do I animate constraint changes?(如何为约束更改设置动画?)
                How to change the preferred language of an iPhone app in iOS 4(如何在 iOS 4 中更改 iPhone 应用程序的首选语言)
                NSLocale currentLocale always returns quot;en_USquot; not user#39;s current language(NSLocale currentLocale 总是返回“en_US;不是用户当前的语言)
                How can I wait for a NSURLConnection delegate to finish before executing the next statement?(在执行下一条语句之前,如何等待 NSURLConnection 委托完成?)
                handle when callback to a dealloced delegate?(何时回调已解除分配的委托?)
                  <tbody id='ylhTP'></tbody>

              2. <tfoot id='ylhTP'></tfoot>
                  <bdo id='ylhTP'></bdo><ul id='ylhTP'></ul>
                  • <i id='ylhTP'><tr id='ylhTP'><dt id='ylhTP'><q id='ylhTP'><span id='ylhTP'><b id='ylhTP'><form id='ylhTP'><ins id='ylhTP'></ins><ul id='ylhTP'></ul><sub id='ylhTP'></sub></form><legend id='ylhTP'></legend><bdo id='ylhTP'><pre id='ylhTP'><center id='ylhTP'></center></pre></bdo></b><th id='ylhTP'></th></span></q></dt></tr></i><div id='ylhTP'><tfoot id='ylhTP'></tfoot><dl id='ylhTP'><fieldset id='ylhTP'></fieldset></dl></div>

                    • <legend id='ylhTP'><style id='ylhTP'><dir id='ylhTP'><q id='ylhTP'></q></dir></style></legend>

                          <small id='ylhTP'></small><noframes id='ylhTP'>