Here's my class graph:

Shape (base class - inherits only from NSObject)
LineShape : Shape
RectangleShape : Shape
SquareShape : RectangleShape

In RectangleShape, I define:

@property (nonatomic, assign) CGSize rectSize;
@property (nonatomic, readonly) CGFloat area;
@property (nonatomic, readonly) CGFloat perimeter;

and in RectangleShape.m, I implement these manually, except for rectSize, which 
I @synthesize:

...
#pragma mark - Non-synthesized accessors

-(CGFloat)area
{
return (self.rectSize.width * self.rectSize.height);
}

-(CGFloat)perimeter
{
return (self.rectSize.width * 2.0 + self.rectSize.height * 2.0);
}
// other stuff...
...

I now want SquareShape to also have the 'area' and 'perimeter' properties, but 
these should be read/write as there's a direct correlation between the area and 
the side of the square; ditto for the perimeter. If a subclass (SquareShape) 
redefines the @property in a superclass as readwrite, are there any 'gotcha's I 
need to watch out for?

i.e.

SquareShape.h
#import "RectangleShape.h"

@interface SquareShape : RectangleShape

@property (nonatomic, readwrite, assign) CGFloat area;
@property (nonatomic, readwrite, assign) CGFloat perimeter;

@end

SquareShape.m
#import "SquareShape.h"

@implementation SquareShape

#pragma mark - Non-synthesized accessors

-(CGFloat)area
{
return (self.rectSize.width * self.rectSize.height);
}

-(void)setArea:(CGFloat)area
{
CGFloat sideLength = sqrt(area);
CGSize squareSize = CGSizeMake(sideLength, sideLength);
self.rectSize = squareSize;
}

-(CGFloat)perimeter
{
return (self.rectSize.width * 2.0 + self.rectSize.height * 2.0);
}

-(void)setPerimeter:(CGFloat)perimeter
{
CGFloat sideLength = perimeter / 4.0;
CGSize squareSize = CGSizeMake(sideLength, sideLength);
self.rectSize = squareSize;
}

@end


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to