为什么要覆盖超类的指定初始值设定项?
我正在阅读“Cocoa Design Pattern”一书及其要点,第三章(两阶段创作)让我很困惑。
确保重写超类的指定初始化程序以调用新的指定初始化程序。
当子类化时,确保每个不是指定初始化程序的新初始化程序都调用指定初始化程序
我的问题是我们如何调用没有参数传递的方法?下面发布了这本书的示例。在这个方法中,作者传递了一些“静态”值,但是我们应该这样做吗?或者这总是可取的?
我的第二个问题是,为什么我必须重写超类的指定方法,因为当我初始化我的对象时我永远不会调用它,除了在我自己指定的初始化程序中,我不会传递任何参数(例如;在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];
}
还请帮助我纠正我的问题,因为我不确定我真正问的是一个正确的问题。
谢谢。
I was reading the book "Cocoa Design Pattern" and 2 of its point, in chapter 3 (Two-Stage Creation) are making me confused.
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?
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.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
指定的初始值设定项是正确配置对象的初始值设定项。如果您不选择一个 init... 方法作为指定的初始化程序,那么您必须确保每个 init... 方法都执行正确的操作。这通常意味着它们都必须具有相同的代码,或者它们都必须调用通用的设置方法。这也意味着任何子类化您的类的人都必须重写所有 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.
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.