菜鸟教程小白 发表于 2022-12-13 09:03:23

objective-c - 我不明白什么时候需要调用被覆盖的方法,或者它只是不需要。


                                            <p><p>我不明白什么时候需要调用被覆盖的方法,或者根本不需要。</p>

<p>例如,对于dealloc,它是必要的</p>

<pre><code>-(void) dealloc
{
   ...
   ;
}
</code></pre>

<p>我猜也是 init..</p>

<pre><code>-(void) init
{
   ;
   ..
}
</code></pre>

<p>viewWillAppear 呢?我应该在我的自定义代码之前还是之后调用 super 方法?</p>

<pre><code>(void)viewWillAppear:(BOOL)animated
{
    ;
}
</code></pre>

<p>谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>确定是否以及何时在重写方法中调用 <code>super</code> 的唯一方法是阅读该方法的文档。 </p>

<p>对于您给出的示例:
<code>初始化</code>:<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/init" rel="noreferrer noopener nofollow">The documentation</a>状态:</p>

<blockquote>
<p>Subclass versions of init need to incorporate the initialization code
for the classes they inherit from, through a message to super:
...
Note that the message to super precedes the initialization code added
in the method. This sequencing ensures that initialization proceeds in
the order of inheritance.</p>
</blockquote>

<p><code>dealloc</code>:<a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-dealloc" rel="noreferrer noopener nofollow">The documentation</a>状态:</p>

<blockquote>
<p>Subclasses must implement their own versions of dealloc to allow the
release of any additional memory consumed by the object—such as
dynamically allocated storage for data or object instance variables
owned by the deallocated object. After performing the class-specific
deallocation, the subclass method should incorporate superclass
versions of dealloc through a message to super:</p>
</blockquote>

<p><code>viewWillAppear:</code> <a href="http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear%3a" rel="noreferrer noopener nofollow">The documentation</a>状态:</p>

<blockquote>
<p>You can override this method to perform custom tasks associated with
presenting the view.<br/>
...
If you override this method, you must call
super at some point in your implementation.</p>
</blockquote>

<p>每种方法都不同。如果您覆盖 <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewDidAppear%3a" rel="noreferrer noopener nofollow">viewDidAppear:</a> ,您<em>必须</em>调用 super。如果您覆盖 <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/loadView" rel="noreferrer noopener nofollow">loadView</a> ,您<em>不得</em>。基本上,任何时候你重写一个方法,你都应该检查那个方法的文档,看看你是否应该调用 super,如果是,你应该在你自己的实现之前还是之后这样做。</p>

<p>如果文档没有说明,则由您决定。先把作者的疏忽大骂一顿之后,做你认为有意义的事。如果文档没有说明,我通常倾向于<em>不</em>调用 super。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 我不明白什么时候需要调用被覆盖的方法,或者它只是不需要。,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7245021/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7245021/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 我不明白什么时候需要调用被覆盖的方法,或者它只是不需要。