본문 바로가기
Object-C

8장 상속

by 풍야 2010. 3. 26.

<상속 예제 #1>

#import <Foundation/Foundation.h>

@interface ClassA : NSObject
{
 int x;
}

- (void) initVar;

@end

@implementation ClassA
- (void) initVar
{
 x=100;
}

@end

@interface ClassB : ClassA // ClassB는 ClassA를 상속한다.

- (void) printVar;

@end

@implementation ClassB

- (void) printVar
{
 NSLog(@"x = %i", x);
}

@end

int main(int argc, char *argv[])
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 ClassB *b = [[ClassB alloc] init];

 [b initVar];
 [b printVar];

 [b release];
 [pool drain];
 return 0;

}


<상속 확장 - 새 메서드 추가>

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
 int width;
 int height;
}

@property int width, height
- (int) area;
- (int) perimeter;

@end

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

-(void) setWidth : (int) w andHeight : (int) h
{
 width = w;
 height = h;
}

-(int) area
{
 return width * height;
}

-(int) perimeter
{
 return (width + height)*2;
}

@end

#import "Rectangle.h"

int main(int argc, char *argv[])
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 Rectangle *myRect = [[Rectangle alloc] init];

 [myRect setWidth : 5 andHeight : 8];

 NSLog(@"Rectangle : w = %i, h = %i", myRect.width,myRect.height);
 NSLog(@"Area = %i, Perimeter = %i h = %i", [myRect.area], [myRect.perimeter);

 [myRect release];

 [pool drain];
 return 0;
}

<상속으로 확장하기>

// <XYPoint.h>

#import <Foundation/Foudation.h>

@interface XYPoint : NSObject
{
 int x;
 int y;
}

@property int x, y;

- (void) setX : (int) xVal andY : (int) yVal;

@end

// <XYPoint.m>

@implementation XYPoint

@synthesize x, y;
- (void) setX : (int) xVal andY : (int) yVal
{
 x=xVal;
 y=yVal;
}

@end

// <Rectangle.h>

@class XYPoint

@interface Rectangle : NSObject
{
 int width;
 int height;
 XYPoint *origin;
}

@property int width, height;

- (XYPoint *) origin;
- (void) setOrigin : (XYPoint *) pt;
- (void) setWidth : (int) w andHeight : (int) h
- (int) area;
- (int) perimeter;

@end

// <Rectangle.m>

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

- (void) setWidth : (int) w andHeight : (int) h
{
 width = w;
 height = h;
}

- (void) setOrigin : (XYPoint *) pt
{
 origin = pt;
// origin = [[XYPoint alloc] init];

// [origin setX : pt.x andY : pt.y];

}

- (int) area
{
 return width * height;
}

- (int) perimeter
{
 return (width+height)*2'
}

- (XYPoint *) origin
{
 return origin;
}

@end

// <main.m>

#import "Rectangle.h"
#import "XYPoint.h"

int main(int argc, char *argv[])
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 Rectangle *myRect = [[Rectangle alloc] init];
 XYPoint *myPoint = [[XYPoint alloc] init];

 [myPoint setX : 100 andY : 200];
 [myRect setWidth : 5 andHeight : 8];

 myRect.origin = myPoint;

 NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height);
 NSLog(@"Origin at (%i,%i)", myRect.origin.x, myRect.origin.y);
 NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);

//  [[myRect origin] release];
 [myRect release];
 [myPoint release];

 [pool drain];

 return 0;
}