为什么覆盖超级类的指定的初始化器? [英] Why override the designated initializer of super class?

查看:139
本文介绍了为什么覆盖超级类的指定的初始化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读书可可设计模式,其中2点,在第3章(两阶段创作)中令我困惑。

I was reading the book "Cocoa Design Pattern" and 2 of its point, in chapter 3 (Two-Stage Creation) are making me confused.


  1. 确保超类指定的初始化程序被覆盖调用新的指定初始化程序。

  1. Make sure that the superclass’ Designated Initializer is overridden to call the new Designated Initializer.

当子类化时,请确保不是指定的初始化程序的每个新初始化程序都调用指定的初始化程序。

When subclassing, make sure every new initializer that isn’t the Designated Initializer calls the Designated Initializer.

我的问题是我们如何调用我们没有参数传递的方法?书中的例子正在下面发布。在这种方法中,作者已经通过了一些静态值,但是我们应该这么做?或者这是总是可取的吗?

My question is how we can call the method for which we don't have the parameters to pass? The book example is being posted below. In this method writer has passed some "static" values, but are we supposed to do this? Or is this always desirable?

我的第二个问题是,为什么我必须覆盖超类的指定方法,当我将永远不会调用它,当我将初始化我的对象,除了在我自己指定的初始化程序中,我不会传递任何参数(例如,在NSObject的情况下)

My second question is, why I have to override the designated method of super class when I will never call it when I will be initializing my object, other than in my own designated initializer, where I will not be passing any parameters (e.g; in case of NSObject)

@interface MYCircle : NSObject {

NSPoint center;

float   radius; 

}

// Designated Initializer 

- (id)initWithCenter:(NSPoint)aPoint radius:(float)aRadius;

@end 

@implementation MYCircle

// Designated Initializer 

- (id)initWithCenter:(NSPoint)aPoint radius:(float)aRadius {

self = [super init];

if(nil != self) {

center = aPoint;

radius = aRadius; 

}

return self; 

}

@end



// Overriden inherited Designated Initializer 
- (id)init {

static const float MYDefaultRadius = 1.0f;

// call Designated Initializer with default arguments

return [self initWithCenter:NSZeroPoint radius:MYDefaultRadius]; 

}

请帮我纠正我的问题,因为我不是确定我真正要问的是一个正确的问题。

Please also help me to correct my question because I am not sure what I am really asking is a correct question.

谢谢。

推荐答案

指定的初始化程序是正确配置对象的初始化程序。如果不选择一个init ...方法作为指定的初始化程序,那么必须确保每个init ...方法都是正确的。这通常意味着它们都必须具有相同的代码,或者它们都必须调用通用的设置方法。这也意味着任何子类化你的类都必须覆盖所有的init ...方法,而不是仅仅一个。

The designated initializer is the one that properly configures the object. If you don't choose one init... method as the designated initializer, then you have to make sure that every init... method does the right thing. That generally means that they all have to have the same code, or they all have to call a common setup method. It also means that anyone subclassing your class has to override all the init... methods instead of just one.

通过选择(即指定)一个init .. 。方法作为所有其他调用的常用方法,您给子类一个单独的重写点和一个方法,它们自己的init ...方法可以调用以确保超类被正确配置。

By picking (i.e. "designating") one init... method as the common method that all the others call, you give subclasses a single override point and a single method that their own init... methods can call to ensure that the superclass is properly configured.

如果您没有调用指定的初始化程序所需的数据,那么您没有设置超类所需的数据。有时您可以选择合理的默认值,如上所述,但如果不是,则无需创建手头的对象。

If you don't have the data necessary to call the designated initializer, then you don't have the data required to set up the superclass. Somestimes you can choose reasonable default values, as above, but if not then it doesn't make any sense to create the object at hand.

这篇关于为什么覆盖超级类的指定的初始化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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