93
+ newWithX: (float)x0 Y: (float)y0;
93
+ (id) newWithX: (float)x0 Y: (float)y0;
95
+ (Point*) pointWithX: (float)x0 Y: (float)y0;
95
97
// instance methods
97
- initWithX: (float)x0 Y: (float)y0;
99
- (id) initWithX: (float)x0 Y: (float)y0;
98
100
- (float) x; // (field accessor)
100
102
- (void) setX: (float)newX;
234
236
Point *lowerLeft;
235
237
float sideLength;
237
+ newWithLowerLeft: (Point *)lowerLeft sideLength: (float)sideLength;
239
+ (id) newWithLowerLeft: (Point *)lowerLeft sideLength: (float)sideLength;
239
- initWithLowerLeft: (Point *)lowerLeft sideLength: (float)sideLength;
241
- (id) initWithLowerLeft: (Point *)lowerLeft sideLength: (float)sideLength;
241
243
- (Point *) lowerLeft;
242
244
- (float) sideLength;
289
291
#import "Point.h"
290
292
@@implementation Point
291
293
// method implementations
294
296
// statements ...
297
+newWithX: (float)x Y: (float)y
299
+ (id)newWithX: (float)x Y: (float)y
299
301
// statements ...
304
- (void) setY: (float)newY
306
- (void)setY: (float)newY
306
308
// statements ...
376
378
Typically, a class will also provide one or more @code{initWith...} methods
377
379
for initialization with arguments, and it may optionally also provide
378
convenience class methods that act like constructors. The general approach
380
@code{+new} methods and convenience class methods that act like constructors.
379
382
to implementing these is illustrated here for the @code{Point} class.
384
Point *point = [[self alloc] init];
385
// note "self" refers to the "Point" _class_ object!
386
return AUTORELEASE(point);
389
// note "self" refers to the "Point" _class_ object!
390
point = [[self allocWithZone: NSDefaultMallocZone()] init];
389
394
+ newWithX: (float)x0 Y: (float)y0
391
Point *point = [[self alloc] initWithX: x Y: y];
398
point = [[self allocWithZone: NSDefaultMallocZone()] initWithX: x Y: y];
406
// note "self" refers to the "Point" _class_ object!
408
return AUTORELEASE(point);
411
+ pointWithX: (float)x0 Y: (float)y0
415
point = [self newWithX: x Y: y];
392
416
return AUTORELEASE(point);
413
437
Notice that, first, the convenience constructors (@code{new} and
414
@code{newWithX:Y:}) execute @code{[self alloc]} to begin with. The
438
@code{newWithX:Y:}) execute @code{[self allocWithZone:]} to begin with. The
415
439
``@code{self}'' here refers to the @i{class} object, since it is used inside a
416
440
@i{class} method. Thus the effect is the same as if ``@code{[Point alloc]}''
417
had been executed in external code. Second, notice that they autorelease the
418
new instance before returning it. This is to follow the rules of memory
441
had been executed in external code. Second, notice that the other
442
convenience constructors (@code{point} and @code{pointWithX:Y:})
443
autorelease the new instance before returning it.
444
This is to follow the rules of memory
419
445
management discussed in @ref{Objects, earlier, Memory Management}. Third,
420
446
note that the @code{new..} methods each call a corresponding @code{init...}
421
447
method. It is not necessary to maintain such a one to one correspondence but
422
448
it is a common convention to have the convenience implementations rely on
423
instance @code{init} methods as shown.
449
instance @code{init} methods as shown. Fourth, note that the use of
450
@code{[self allocWithZone: NSDefaultMallocZone()]} rather than
451
@code{[self alloc]} is generally unnecessary, but provides a slight
452
efficiency gain since @code{+alloc} is implemented by calling
453
@code{+allocWithZone:} on the default zone.
425
455
@b{Designated Initializer}