- (MyClass *) initWith: (int) n: (int) m
{
self = [super init] ;/ / books with this line 1
/ / [Self init]; / / test also passed 2
if (self)
{
[Self setNum1: n andNum2: m];
}
return self;
}
Will, 1,2 two are equivalent? Why book to call the super's init??
Reply:
self = [super init] ;/ / books with this line 1
/ / [Self init]; / / test also passed 2
About 1: super init is the parent class initialization function call, if the child class needs certain resources of the parent class is needed so, self = [super init], refers to the resources
subclass pointer to the parent classAbout 2: initialize the subclass of resources, such as sub-class has a NSString * str = [[NSString alloc] init]; would call this method, in fact, inside the init function,
- (Id) init
{Return self;} also can run, but if the engineering of complex words can not, as a subclass of the extensive use of parent resources, which is why there are [super dealloc] reason in subclass dealloc in another, OC 2 stage construction and alloc and init are separate controls already allocated before init, init just finished initializing it, hope to understand, it is recommended to rewrite their own init try more like a
Reply:
- (MyClass *) initWith: (int) n: (int) m
{
self = [super init] ;/ / write, said: only through the initialization with a call init no corresponding initialization.
/ / [Self init]; / / if the class does not implement its own init function is equivalent to the one sentence.
if (self)
{
[Self setNum1: n andNum2: m];
}
return self;
}
No comments:
Post a Comment