§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ printf( "hello world\n" );
§ return 0;
}
hello world
§ #import <Foundation/NSObject.h>
§
§ @interface Fraction: NSObject {
§ int numerator;
§ int denominator;
§ }
§
§ -(void) print;
§ -(void) setNumerator: (int) n;
§ -(void) setDenominator: (int) d;
§ -(int) numerator;
§ -(int) denominator;
§ @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;
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] init];
§
§ // set the values
§ [frac setNumerator: 1];
§ [frac setDenominator: 3];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "\n" );
§
§ // free memory
§ [frac release];
§
§ return 0;
}
The fraction is: 1/3
§ ...
§ -(void) setNumerator: (int) n andDenominator: (int) d;
...
§ ...
§ -(void) setNumerator: (int) n andDenominator: (int) d {
§ numerator = n;
§ denominator = d;
§ }
...
§ #import <stdio.h>
§ #import "Fraction.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] init];
§ Fraction *frac2 = [[Fraction alloc] init];
§
§ // set the values
§ [frac setNumerator: 1];
§ [frac setDenominator: 3];
§
§ // combined set
§ [frac2 setNumerator: 1 andDenominator: 5];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "\n" );
§
§ // print it
§ printf( "Fraction 2 is: " );
§ [frac2 print];
§ printf( "\n" );
§
§ // free memory
§ [frac release];
§ [frac2 release];
§
§ return 0;
}
§ The fraction is: 1/3
Fraction 2 is: 1/5
§ ...
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
...
§ ...
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
§ self = [super init];
§
§ if ( self ) {
§ [self setNumerator: n andDenominator: d];
§ }
§
§ return self;
§ }
...
§ #import <stdio.h>
§ #import "Fraction.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] init];
§ Fraction *frac2 = [[Fraction alloc] init];
§ Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§ // set the values
§ [frac setNumerator: 1];
§ [frac setDenominator: 3];
§
§ // combined set
§ [frac2 setNumerator: 1 andDenominator: 5];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "\n" );
§
§ printf( "Fraction 2 is: " );
§ [frac2 print];
§ printf( "\n" );
§
§ printf( "Fraction 3 is: " );
§ [frac3 print];
§ printf( "\n" );
§
§ // free memory
§ [frac release];
§ [frac2 release];
§ [frac3 release];
§
§ return 0;
}
§ The fraction is: 1/3
§ Fraction 2 is: 1/5
Fraction 3 is: 3/10
§ #import <Foundation/NSObject.h>
§
§ @interface Access: NSObject {
§ @public
§ int publicVar;
§ @private
§ int privateVar;
§ int privateVar2;
§ @protected
§ int protectedVar;
§ }
@end
§ #import "Access.h"
§
§ @implementation Access
@end
§ #import "Access.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ Access *a = [[Access alloc] init];
§
§ // works
§ a->publicVar = 5;
§ printf( "public var: %i\n", a->publicVar );
§
§ // doesn't compile
§ //a->privateVar = 10;
§ //printf( "private var: %i\n", a->privateVar );
§
§ [a release];
§ return 0;
}
public var: 5
§ #import <Foundation/NSObject.h>
§
§ static int count;
§
§ @interface ClassA: NSObject
§ +(int) initCount;
§ +(void) initialize;
@end
§ #import "ClassA.h"
§
§ @implementation ClassA
§ -(id) init {
§ self = [super init];
§ count++;
§ return self;
§ }
§
§ +(int) initCount {
§ return count;
§ }
§
§ +(void) initialize {
§ count = 0;
§ }
@end
§ #import "ClassA.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ ClassA *c1 = [[ClassA alloc] init];
§ ClassA *c2 = [[ClassA alloc] init];
§
§ // print count
§ printf( "ClassA count: %i\n", [ClassA initCount] );
§
§ ClassA *c3 = [[ClassA alloc] init];
§
§ // print count again
§ printf( "ClassA count: %i\n", [ClassA initCount] );
§
§ [c1 release];
§ [c2 release];
§ [c3 release];
§
§ return 0;
}
§ ClassA count: 2
ClassA count: 3