J'ai eu un exercice pour surcharger la méthode init, donc j'ai besoin de créer un fichier init
qui définira également certains attributs.
Ma question est la suivante : pourquoi ai-je besoin de définir l'original init
au cas où la nouvelle méthode init
ne fonctionne pas ?
C'est mon .h
fichier :
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle: NSObject
@property float width, height, tx, ty;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) translate: (XYPoint *) point;
-(id) initWithWidth:(int) w andHeight:(int) h;
-(id) init;
@end
Et .m
(uniquement les méthodes init) :
-(id) initWithWidth:(int)w andHeight:(int)h
{
self = [super init];
if (self)
{
[self setWidth:w andHeight:h];
}
return self;
}
-(id) init
{
return [self initWithWidth:0 andHeight:0];
}
Je sais que c'est bien comme ça, mais si quelqu'un peut m'expliquer pourquoi, je l'apprécierais.