IOS 小例子,输出一个图形的坐标,颜色,形状-2。

//
//  main.m
//  0927-inherit
//
//  Created by panba on 15-9-27.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef enum{
    kyuanxing,
    kfangxing,
    ktuoyuan
} shapeType ;//shapeType

typedef enum{
    kred,
    kgreen,
    kblue
} shapeColor; //shapeColor

typedef struct{
    int x,y,width,height;
}shapeRect; //shapeRect

typedef struct{
    shapeType type;
    shapeColor color;
    shapeRect rect;
} Shape; //Shape

NSString *returnTypeName(shapeType typeValue)
{
    switch (typeValue) {
        case kfangxing:
            return @"fangxing";
            break;
        case ktuoyuan:
            return @"tuoyuan";
            break;
        case kyuanxing:
            return @"yuanxing";
            break;
        default:
            break;
    }
} //nsstring *returnTypeName

NSString *returnColorName(shapeColor colorValue)
{
    switch (colorValue) {
        case kblue:
            return @"blue";
            break;
        case kgreen:
            return @"green";
            break;
        case kred:
            return @"red";
            break;
        default:
            break;
    }
} //nsstirng *returnColorName

@interface ClassShape : NSObject
{
@private
    shapeType typeName;
    shapeColor colorName;
    shapeRect rectName;
}
-(void) setRect:(shapeRect )ClassRectValue;
-(void) setColor:(shapeColor )ClassColorValue;
-(void) setType:(shapeType )ClassTyprValue;
-(void) draw;
@end

@implementation ClassShape:NSObject
-(void) setRect:(shapeRect )imRectValue
{
    rectName = imRectValue;
}
-(void) setColor:(shapeColor)imColorValue
{
    colorName = imColorValue;
}
-(void) setType:(shapeType)imTypeValue
{
    typeName = imTypeValue;
}
-(void) draw
{
    NSLog(@"type is %@ at (%i,%i,%i,%i) in %@",returnTypeName(typeName),rectName.x,rectName.y,rectName.width,
          rectName.height,returnColorName(colorName));
} //draw
@end

// typeYuanxing:classShape
@interface TypeYuanxing : ClassShape
@end
@implementation TypeYuanxing:ClassShape
@end

//typeFangxing:classShape
@interface TYpeFangxing : ClassShape
@end
@implementation TYpeFangxing:ClassShape
@end

//typeTuoyuan:classShape
@interface TypeTuoyuan : ClassShape
@end
@implementation TypeTuoyuan:ClassShape
@end

//drawshapes  如果这里报//must explicitly descrilbe intended ownersship of an object array parameter,请把arc关掉。
void drawshapes(id shapes[],int count)
{
    int i  ;
    for (i = 0; i<count; i++) {
        id shape = shapes[i];
        [shape draw];
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        id shapes[3];//这里使用id类型,因为id可以接受所有 的类型 
        
        shapeRect rect0 = {0,0,100,200};
        shapes[0] = [TYpeFangxing new];
        [shapes[0] setType:kfangxing];
        [shapes[0] setColor:kred];
        [shapes[0] setRect:rect0];
        
        shapeRect rect1 = {20,30,40,50};
        shapes[1] = [TypeYuanxing new];
        [shapes[1] setType:kyuanxing];
        [shapes[1] setColor:kgreen];
        [shapes[1] setRect:rect1];
        
        shapeRect rect2 = {10,23,50,88};
        shapes[2] = [TypeTuoyuan new];
        [shapes[2] setType:ktuoyuan];
        [shapes[2] setColor:kblue];
        [shapes[2] setRect:rect2];
        
        drawshapes(shapes, 3);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y型树杈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值