《OC基础教程》读书笔记2-面向对象

接口

以@interface开始,@end结束

@interface Circle:NSObject{

int radius;

NSString* fillColor;

}

-(void)setRadius:(int)r;

-(void)setFillColor:(NSString*)color;

-(void)draw;

@end;


//:NSObject表示该类继承自NSObject;

//{}中放实例变量,没有实例变量可省略,这与很多语言不一样!


实现

@implementation Circle

-(void)setRadius:(int)r{

radius = r;

}

-(void)setFillColor:(NSString*)color{

fillColor = color;

}

-(void)draw{

NSLog(@"this radius is %d",radius);

NSLog(@"this fillColor is %@",fillColor);

}

@end;


实例化

id c = [Circle new];

[c setRadius:3];

[c setFillColor:@"red"];

[c draw];

你可能感兴趣的:(IOS,Object,C)