C언어와 Objective-C의 Mixing 하여 사용한 상태
//
// main.m
// Shape-OOP
//
// Created by ktds on 11. 8. 2..
// Copyright 2011 ktds. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef enum
{
kRedColor,
kGreenColor,
kBlueColor
} ShapeColor;
typedef struct
{
int x;
int y;
int width;
int height;
} ShapeRect;
NSString *colorName(ShapeColor color);
void drawShapes(id *shapes, int count);
@interface Circle : NSObject {
ShapeColor fillColor;
ShapeRect bounds;
}
-(void) setFillColor : (ShapeColor) fillColor;
-(void) setBounds : (ShapeRect) bounds;
-(void) draw;
@end
@implementation Circle
-(void) setFillColor:(ShapeColor) c
{
fillColor = c;
}
-(void) setBounds:(ShapeRect) b
{
bounds = b;
}
-(void) draw
{
NSString *strColor = colorName(fillColor);
NSLog(@"좌표(%d,%d), 폭:%d, 높이:%d 영역에 %@ 원을 그립니다", bounds.x, bounds.y, bounds.width, bounds.height, strColor);
}
@end
@interface Rectangle : NSObject {
ShapeColor fillColor;
ShapeRect bounds;
}
-(void) setFillColor : (ShapeColor) fillColor;
-(void) setBounds : (ShapeRect) bounds;
-(void) draw;
@end
@implementation Rectangle
-(void) setFillColor:(ShapeColor) c
{
fillColor = c;
}
-(void) setBounds:(ShapeRect) b
{
bounds = b;
}
-(void) draw
{
NSString *strColor = colorName(fillColor);
NSLog(@"좌표(%d,%d), 폭:%d, 높이:%d 영역에 %@ 사각형을 그립니다", bounds.x, bounds.y, bounds.width, bounds.height, strColor);
}
@end
@interface Egg : NSObject {
ShapeColor fillColor;
ShapeRect bounds;
}
-(void) setFillColor : (ShapeColor) fillColor;
-(void) setBounds : (ShapeRect) bounds;
-(void) draw;
@end
@implementation Egg
-(void) setFillColor:(ShapeColor) c
{
fillColor = c;
}
-(void) setBounds:(ShapeRect) b
{
bounds = b;
}
-(void) draw
{
NSString *strColor = colorName(fillColor);
NSLog(@"좌표(%d,%d), 폭:%d, 높이:%d 영역에 %@ 타원을 그립니다", bounds.x, bounds.y, bounds.width, bounds.height, strColor);
}
@end
int main(int argc, const char * argv[])
{
id shapes[3];
shapes[0] = [[Circle alloc] init];
shapes[1] = [[Rectangle alloc] init];
shapes[2] = [[Egg alloc] init];
// shapes[0] = [Circle new];
// shapes[1] = [Rectangle new];
// shapes[2] = [Egg new];
ShapeRect rect0 = {0, 0, 10, 30};
ShapeRect rect1 = {30, 40, 50, 60};
ShapeRect rect2 = {15, 19, 37, 29};
[shapes[0] setBounds:rect0];
[shapes[1] setBounds:rect1];
[shapes[2] setBounds:rect2];
[shapes[0] setFillColor:kRedColor];
[shapes[1] setFillColor:kGreenColor];
[shapes[2] setFillColor:kBlueColor];
int count = sizeof(shapes) / sizeof(shapes[0]);
drawShapes(shapes, count);
return 0;
}
NSString *colorName(ShapeColor color)
{
switch(color)
{
case kRedColor : return @"빨강색"; break;
case kGreenColor : return @"녹색"; break;
case kBlueColor : return @"파랑색"; break;
}
return @"지정된 색 없음";
}
void drawShapes(id *shapes, int count)
{
for(int i = 0 ; i < count ; i++)
{
[shapes[i] draw];
}
}