object-c笔记一

(本笔记只是简单的记录,只给自己看)

接口的定义:

@interface Circle:NSObject
{
	ShapeColor fillColor;
	ShapeRect bounds;
}
-(void)setFillColor:(ShapeColor)fillColor;
-(void)setBounds:(ShapeRect)bounds;
-(void)draw;
@end

 

 实现 :

@implementation Circle
-(void)setFillColor:(ShapeColor)c
{
	fillColor = c;
} //setFillColor

-(void)setBounds:(ShapeRect)b
{
	bounds = b;
} //setBounds

-(void)draw
{
	NSLog(@"drawing a circle at
		(%d %d %d %d) in %@",
		bounds.x,bounds.y,bounds.width,
		bounds.height,colorName(fillColor));
}//draw

 

 实例化:

int main(int argc,const char* argv[])
{
	id shape[1]; //定义类型为id指针,长度为1名字为shapes的数组
	ShapeRect rect0 = {0,0,10,30}; //定义一个矩形并为其提交4个坐标点参数
	shape[0] = [Circle new]; //为数组第一个元素实例化一个Circle类
	[shapes[0] setBounds:rect0]; // 数组元素调用Circle类的方法
	[shapes[0] setFillColor:kRedColor];
	[shapes[0] draw];
	return (0);
}

 

 

继承:Objective-C中只能继承一个类

 

你可能感兴趣的:(object)