[Objective-c基础教程] 第三章

接口代码如下:

@interface Cicle : NSObject //接口名称后面的:表示继承父类,有父类的所有行为。

{

ShapeColor fillColor;

ShapeRect bounds; //定义实例变量

}

- (void) setFillColor : (ShapeColor)  fillColor; //Objective-c 可以返回与C相同的类型,包括:标准类型(整形、浮点型、字符型)、指针、对象引用和结构。

- (void) setBounds : (ShapeRect)  bounds;

- (void) draw; //方法的声明,短线表示Objective-c 方法的声明,区分函数声明与方法声明的方式,函数声明中没有短线。

@end //Cicle ,最后注释

实现代码如下:

@implementation Cicle // @implementation是一个编译指令,表明将为某个类提供代码,在@implementation中有,而@interface没有的,视为私有方法。

- (void) setFillColor : (ShapeColor)  c //如果这里参数名用fillColor,就会隐藏fillColor的实例变量,编译器会有警告信息。

{

fillColor = c;

}

- (void) setBounds : (ShapeRect)  b

{

bounds = b;

}

- (void) draw {

NSLog(@"drawing a circle at (%d %d %d %d) in %@", bounds.x ,bounds.y ,bounds.width ,bounds.height ,colorName(fillColor));

}

@end //Circle

实例化对象:

id shape;// id 指向某种对象的指针

shape = [Circle new ]

posted on 2012-02-13 16:43  jackbutler  阅读(149)  评论(0编辑  收藏  举报

导航