日期:2014-05-17 浏览次数:20727 次
#import <stdio.h> int main(int argc,const char *argv[]){ printf("hello world/n"); return 0; }
#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; }