objective-c 类使用举例!

来源:互联网 发布:阿里云流量计费标准 编辑:程序博客网 时间:2024/04/30 08:59

Objective-C中一个简单类的应用:

#inclued <stdio.h>
#include <Foundation/Foundation.h>

//声明类的接口
@interface  Container:NSObject
{
int number;
}
-(void)setNumber:(int) n;
-(int)intValue;
@#end

//定义类
@implementation  Container
-(void)setNumber:(int)n
{
number=n;
}
-(int) intValue
{
return   number;
}
@end

//主函数
int  main(void)
{
Container *object=[Container new];  //创建一个对象
[object setNumber:5];
printf("The number is %i\n", [object   intValue]);
return 0;
}

输出结果:
The number is 5

0 0
原创粉丝点击