日期:2014-05-17 浏览次数:20887 次
#import <stdio.h> int main(int argc,const char *argv[]){ printf("hello world\n"); return 0; }
samsung@coco ~ $ gcc hello.m samsung@coco ~ $ ls a.exe hello.m samsung@coco ~ $ ./a.exe hello world samsung@coco ~
#import <Foundation/NSObject.h> @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) d; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; -(void) setNumerator: (int) n ddd: (int)d; -(void) setNumerator: (int)n : (int)d :(int) a; // 这里,有3个setNumerator函数, 是重载。 @end
#import "Fraction.h" #import <stdio.h> @implementation Fraction -(void) print { printf( "%i/%i", numerator, denominator ); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } -(int) denominator { return denominator; } -(int) numerator { return numerator; } -(void) setNumerator: (int) n ddd: (int)d { numerator = n; denominator = d; } -(void) setNumerator: (int)n : (int)d :(int) a { numerator = n; denominator = d; printf("+++++a = %d +++ \n", a); } @end
#import <stdio.h> #import <Foundation/Foundation.h> #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; int x; int y; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n\n" ); NSLog(@"hello world!!!\n"); // ok [frac setNumerator:34 ddd: 98]; [frac print]; printf( "\n\n" ); NSLog(@"hello world world!!!\n"); // ok [frac setNumerator:44 : 55 :66]; // ok [frac print]; printf( "\n\n" ); scanf("%d %d", &x,&y); //scanf 函数的测试,ok [frac setNumerator: x ddd: y]; //ok [frac print]; // free memory [frac release]; // [frac release]; //前面已经release了,所以这里发生异常:程序崩溃。 //即对空指针进行release,当然不允许了。 return 0; }
samsung@coco ~/objc/fraction $ ./main.exe The fraction is: 1/3 2010-08-13 16:29:01.515 main[1212] hello world!!! 34/98 2010-08-13 16:29:01.515 main[1212] hello world world!!! +++++a = 66 +++ 44/55 22 33 22/33 samsung@coco