先写一个hello.m
#import <Foundation/Foundation.h>
@interface Fraction:NSObject{
int number;
int ids;
}
-(void) print;
-(void) setNumber:(int)n;
-(void) setIds:(int)ids;
@end
@implementation Fraction
-(void) print{
NSLog(@"(x=%i,y=%i)",number,ids);
}
-(void) setNumber:(int) n{
number=n;
}
-(void) setIds:(int)ids{
ids=ids;
}
@end
int main (int argc, const char *argv[]) {
NSLog(@"Hello World!\n");
// printf("%s\n","hello,ducker");
Fraction *fract=[[Fraction alloc] init];
[fract setNumber : 10];
printf("(number=%i)",fract->number);
[fract release];
return 0;
}
写一个编译脚本oc
$ cat oc
#!/bin/bash
gcc -o $1 $2 \
-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/ \
-L /GNUstep/System/Library/Libraries/ \
-lobjc \
-lgnustep-base
编译:
./oc hello hello.m
然后执行:
./hello.exe
$ ./hello.exe
2012-04-09 18:26:02.500 hello[2500] Hello World!
(number=10)
稍后继续object-c研究