Object-C: 学习实例之继承

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值