Test.h:
#import <Foundation/Foundation.h> @interface Test : NSObject { int intX; int intY; } -(void) print; -(void) setIntX:(int) n; -(void) setIntY:(int) d; -(int) intX; -(int) intY; @end
Test.m:
#import "Test.h" @implementation Test -(void) print { NSLog(@"int add result: %i",intX+intY); } -(void) setIntX:(int)n { intX=n; } -(void) setIntY:(int)d { intY=d; } -(int) intX { return intX; } -(int) intY { return intY; } @end
main.m:
#import <Foundation/Foundation.h> #import "Test.h" int main(int argc,const char *argv[]) { @autoreleasepool { Test *test=[[Test alloc] init]; [test setIntX:1]; [test setIntY:1]; [test print]; } return 0; }
console 输出:
2013-09-18 14:05:21.354 Obj-c[1228:303] int add result: 2