main.m
#import <Foundation/Foundation.h>
typedef enum{
kRedColor,
kGreenColor,
kBlueColor
} ShapeColor;
typedef struct{
int x, y, width, height;
} ShapeRect;
NSString *colorName(ShapeColor colorName)
{
switch(colorName)
{
case kRedColor:
return @"red";
break;
case kGreenColor:
return @"green";
break;
case kBlueColor:
return @"blue";
break;
}
return @"no color";
}
@interface Shape:NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}
- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;
@end //Shape
@implementation Shape
- (void) setFillColor: (ShapeColor) c
{
fillColor = c;
}
- (void) setBounds: (ShapeRect) b
{
bounds = b;
}
- (void) draw
{
NSLog(@"drawing a Shape at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
}
@end
@interface Circle: Shape
@end
@interface yyRectangle: Shape
@end
@interface Egg: Shape
@end
@implementation Circle
- (void) draw
{
NSLog(@"drawing a Circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
}
@end
@implementation yyRectangle
- (void) draw
{
NSLog(@"drawing a Rectangle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
}
@end
@implementation Egg
- (void) draw
{
NSLog(@"drawing a Egg at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
}
@end
void drawShapes(id shapes[], int n)
{
int i;
for(i=0; i<n; i++)
{
[shapes[i] draw];
}
}
int main(int argc, const char *argv[])
{
id shapes[3];
ShapeRect rect0 = {0, 0, 10, 30};
shapes[0] = [Circle new];
[shapes[0] setBounds: rect0];
[shapes[0] setFillColor: kRedColor];
ShapeRect rect1 = {30, 40, 50, 60};
shapes[1] = [yyRectangle new];
[shapes[1] setBounds: rect1];
[shapes[1] setFillColor: kGreenColor];
ShapeRect rect2 = {15, 19, 37, 29};
shapes[2] = [Egg new];
[shapes[2] setBounds: rect2];
[shapes[2] setFillColor: kBlueColor];
drawShapes(shapes, 3);
return 0;
}
Makefile
######################################################################
#
# 文件名: Makefile
# 描述: 生成exe
# 日期: 2013-11-09 11:30
#
######################################################################
CC = gcc
CFLAGS = -fconstant-string-class=NSConstantString
LFLAGS = -lobjc -lgnustep-base
LIB_HDRS = /GNUstep/System/Library/Headers
LIB_LIBS = /GNUstep/System/Library/Libraries
BIN = main.exe
all: $(BIN)
$(BIN): main.o
$(CC) -o $@ $< -L$(LIB_LIBS) $(LFLAGS)
main.o: main.m
$(CC) -I $(LIB_HDRS) $(CFLAGS) -c $<
clean:
rm -f $(BIN) *.o