-(BOOL) isKindOfClass: classObj |
is object a descendent or member of classObj |
-(BOOL) isMemberOfClass: classObj |
is object a member of classObj |
-(BOOL) respondsToSelector: selector |
does the object have a method named specifiec by the selector |
+(BOOL) instancesRespondToSelector: selector |
does an object created by this class have the ability to respond to the specified selector |
-(id) performSelector: selector |
invoke the specified selector on the object |
§ #import "Square.h"
§ #import "Rectangle.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
§ Square *sq = [[Square alloc] initWithSize: 15];
§
§ // isMemberOfClass
§
§ // true
§ if ( [sq isMemberOfClass: [Square class]] == YES ) {
§ printf( "square is a member of square class\n" );
§ }
§
§ // false
§ if ( [sq isMemberOfClass: [Rectangle class]] == YES ) {
§ printf( "square is a member of rectangle class\n" );
§ }
§
§ // false
§ if ( [sq isMemberOfClass: [NSObject class]] == YES ) {
§ printf( "square is a member of object class\n" );
§ }
§
§ // isKindOfClass
§
§ // true
§ if ( [sq isKindOfClass: [Square class]] == YES ) {
§ printf( "square is a kind of square class\n" );
§ }
§
§ // true
§ if ( [sq isKindOfClass: [Rectangle class]] == YES ) {
§ printf( "square is a kind of rectangle class\n" );
§ }
§
§ // true
§ if ( [sq isKindOfClass: [NSObject class]] == YES ) {
§ printf( "square is a kind of object class\n" );
§ }
§
§ // respondsToSelector
§
§ // true
§ if ( [sq respondsToSelector: @selector( setSize: )] == YES ) {
§ printf( "square responds to setSize: method\n" );
§ }
§
§ // false
§ if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) {
§ printf( "square responds to nonExistant method\n" );
§ }
§
§ // true
§ if ( [Square respondsToSelector: @selector( alloc )] == YES ) {
§ printf( "square class responds to alloc method\n" );
§ }
§
§ // instancesRespondToSelector
§
§ // false
§ if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) {
§ printf( "rectangle instance responds to setSize: method\n" );
§ }
§
§ // true
§ if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) {
§ printf( "square instance responds to setSize: method\n" );
§ }
§
§ // free memory
§ [rec release];
§ [sq release];
§
§ return 0;
}
§ square is a member of square class
§ square is a kind of square class
§ square is a kind of rectangle class
§ square is a kind of object class
§ square responds to setSize: method
§ square class responds to alloc method
square instance responds to setSize: method
§ #import "Fraction.h"
§
§ @interface Fraction (Math)
§ -(Fraction*) add: (Fraction*) f;
§ -(Fraction*) mul: (Fraction*) f;
§ -(Fraction*) div: (Fraction*) f;
§ -(Fraction*) sub: (Fraction*) f;
@end
§ #import "FractionMath.h"
§
§ @implementation Fraction (Math)
§ -(Fraction*) add: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator] +
§ denominator * [f numerator]
§ denominator: denominator * [f denominator]];
§ }
§
§ -(Fraction*) mul: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f numerator]
§ denominator: denominator * [f denominator]];
§
§ }
§
§ -(Fraction*) div: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator]
§ denominator: denominator * [f numerator]];
§ }
§
§ -(Fraction*) sub: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator] -
§ denominator * [f numerator]
§ denominator: denominator * [f denominator]];
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "FractionMath.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3];
§ Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5];
§ Fraction *frac3 = [frac1 mul: frac2];
§
§ // print it
§ [frac1 print];
§ printf( " * " );
§ [frac2 print];
§ printf( " = " );
§ [frac3 print];
§ printf( "\n" );
§
§ // free memory
§ [frac1 release];
§ [frac2 release];
§ [frac3 release];
§
§ return 0;
}
1/3 * 2/5 = 2/15
§ #import <Foundation/NSObject.h>
§
§ @interface MyClass: NSObject
§ -(void) publicMethod;
@end
§ #import "MyClass.h"
§ #import <stdio.h>
§
§ @implementation MyClass
§ -(void) publicMethod {
§ printf( "public method\n" );
§ }
§ @end
§
§ // private methods
§ @interface MyClass (Private)
§ -(void) privateMethod;
§ @end
§
§ @implementation MyClass (Private)
§ -(void) privateMethod {
§ printf( "private method\n" );
§ }
@end
§ #import "MyClass.h"
§
§ int main( int argc, const char *argv[] ) {
§ MyClass *obj = [[MyClass alloc] init];
§
§ // this compiles
§ [obj publicMethod];
§
§ // this throws errors when compiling
§ //[obj privateMethod];
§
§ // free memory
§ [obj release];
§
§ return 0;
}
public method
§ #import "Fraction.h"
§
§ @interface FractionB: Fraction
§ -(void) print;
§ @end
§ #import "FractionB.h"
§ #import <stdio.h>
§
§ @implementation FractionB
§ -(void) print {
§ printf( "(%i/%i)", numerator, denominator );
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "FractionB.h"
§
§ int main( int argc, const char *argv[] ) {
§ Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "\n" );
§
§ // make FractionB pose as Fraction
§ [FractionB poseAsClass: [Fraction class]];
§
§ Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac2 print];
§ printf( "\n" );
§
§ // free memory
§ [frac release];
§ [frac2 release];
§
§ return 0;
}
§ The fraction is: 3/10
The fraction is: (3/10)
§ @protocol Printing
§ -(void) print;
@end
§ #import <Foundation/NSObject.h>
§ #import "Printing.h"
§
§ @interface Fraction: NSObject <Printing, NSCopying> {
§ int numerator;
§ int denominator;
§ }
§
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
§ -(void) setNumerator: (int) d;
§ -(void) setDenominator: (int) d;
§ -(void) setNumerator: (int) n andDenominator: (int) d;
§ -(int) numerator;
§ -(int) denominator;
@end
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ @implementation Fraction
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
§ self = [super init];
§
§ if ( self ) {
§ [self setNumerator: n andDenominator: d];
§ }
§
§ return self;
§ }
§
§ -(void) print {
§ printf( "%i/%i", numerator, denominator );
§ }
§
§ -(void) setNumerator: (int) n {
§ numerator = n;
§ }
§
§ -(void) setDenominator: (int) d {
§ denominator = d;
§ }
§
§ -(void) setNumerator: (int) n andDenominator: (int) d {
§ numerator = n;
§ denominator = d;
§ }
§
§ -(int) denominator {
§ return denominator;
§ }
§
§ -(int) numerator {
§ return numerator;
§ }
§
§ -(Fraction*) copyWithZone: (NSZone*) zone {
§ return [[Fraction allocWithZone: zone] initWithNumerator: numerator
§ denominator: denominator];
§ }
@end
§ #import <Foundation/NSObject.h>
§ #import "Printing.h"
§
§ @interface Complex: NSObject <Printing> {
§ double real;
§ double imaginary;
§ }
§
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i;
§ -(void) setReal: (double) r;
§ -(void) setImaginary: (double) i;
§ -(void) setReal: (double) r andImaginary: (double) i;
§ -(double) real;
§ -(double) imaginary;
@end
§ #import "Complex.h"
§ #import <stdio.h>
§
§ @implementation Complex
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i {
§ self = [super init];
§
§ if ( self ) {
§ [self setReal: r andImaginary: i];
§ }
§
§ return self;
§ }
§
§ -(void) setReal: (double) r {
§ real = r;
§ }
§
§ -(void) setImaginary: (double) i {
§ imaginary = i;
§ }
§
§ -(void) setReal: (double) r andImaginary: (double) i {
§ real = r;
§ imaginary = i;
§ }
§
§ -(double) real {
§ return real;
§ }
§
§ -(double) imaginary {
§ return imaginary;
§ }
§
§ -(void) print {
§ printf( "%_f + %_fi", real, imaginary );
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "Complex.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§ Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];
§ id <Printing> printable;
§ id <NSCopying, Printing> copyPrintable;
§
§ // print it
§ printable = frac;
§ printf( "The fraction is: " );
§ [printable print];
§ printf( "\n" );
§
§ // print complex
§ printable = comp;
§ printf( "The complex number is: " );
§ [printable print];
§ printf( "\n" );
§
§ // this compiles because Fraction comforms to both Printing and NSCopyable
§ copyPrintable = frac;
§
§ // this doesn't compile because Complex only conforms to Printing
§ //copyPrintable = comp;
§
§ // test conformance
§
§ // true
§ if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {
§ printf( "Fraction conforms to NSCopying\n" );
§ }
§
§ // false
§ if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {
§ printf( "Complex conforms to NSCopying\n" );
§ }
§
§ // free memory
§ [frac release];
§ [comp release];
§
§ return 0;
}
§ The fraction is: 3/10
§ The complex number is: 5.000000 + 15.000000i
Fraction conforms to NSCopying