代码清单1
#import <Foundation/Foundation.h>
//@interface部分 描述类和类的方法
@interface Fraction:NSObject
-(void) print;
-(void) setA:(int) a1;
-(void) setB:(int) b1;
@end
//@implementation部分 描述数据
@implementation Fraction
{
int a;
int b;
}
-(void) print
{
NSLog(@"%i/%i",a,b);
}
-(void) setA:(int) a1
{
a=a1;
}
-(void) setB:(int) b1
{
b=b1;
}
@end
int main(int argc,char *argv[])
{
@autoreleasepool{
Fraction *f;//定义对象
f=[Fraction alloc];//分配内存
f=[f init];//初始化对象->
//f=[[Fraction alloc] init];
//Fraction *f=[[Fraction alloc] init];
[f setA:1];//向setA发送消息,并提供一个值为1的参数,存储到a1中
[f setB:3];
NSLog(@"The num is");
[f print];//试用消息表达式调用print方法
}
return 0;
}