启程 Objectvie-C
2010-12-30 04:38:25 来源:WEB开发网2
#import
int main(int argc,const char*arg【】)
{
const char *words【4】={"abcde","def","jxyzs","kl"};
int wordCount=4;
int i;
for(i=0;i< p>
{
NSLog(@"%s is %d characters long",words[i],strlen(words[i]));
}
return(0);
}
3.现在可以开始oop 了
类:一种结构,表示对象的类型;
对象:一种结构,它包含值和指向其他类的隐藏指针;
实例:比如circle对象也可以称作circle类的一个实例;
消息:对象可以执行的操作;
方法:响应消息而运行的代码;消息可以调用多个方法;
接口:是对象的类应该提供特性的描述;
实现:是使接口正常工作的代码;
。。下面来一个标准的OOP
#interface Circle:NSObject //这是为命名为Circle的新类定义的接口。
{
ShapeColor fillColor;
ShapeRect bounds;
}
-(void)setFillColor:(shapeColor)fillColor; //(返回值) 方法 :(参数类型 )参数
-(void)setBounds:(shapeRect)bounds;
-(void)draw
@end
#implementation Circle //定义类的公共接口。
-(void)setFillColor:(shapeColor) C
{
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.widh,bounds.height,
colorName(fillColor);
}
@end
int main(int argc,const char *argv[]) //实例化,
{
id shapes[3];
ShaeRect rect0={0.0.10.30};
shapes[0]=[Circle new];
[shapes[0] setBounds: rect0];
[shapes[0] setFillColor:kRedColor];
ShaeRect rect1={30.40.50.36};
shapes[1]=[Rectangle new];
[shapes[1] setBounds: rect1;
[shapes[1] setFillColor:kGreenColor];
ShaeRect rect2={15.19.37.29};
shapes[2]=[OblateSphereoid new];
[shapes[2] setBounds: rect2];
[shapes[2] setFillColor:kBlueColor];
drawShapes (shapes.3);
return(0);
}
更多精彩
赞助商链接