//
// 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;
}