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

查看:40
本文介绍了为什么初始化子类需要调用超类的相同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函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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