forked from mshafae/CS411SampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_binding.m
126 lines (112 loc) · 3.12 KB
/
dynamic_binding.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// $Id: dynamic_binding.m 3142 2011-09-19 09:42:50Z mshafae $
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void) doSomething;
@end
@implementation Person
- (void) doSomething
{
NSLog( @"Person does something" );
}
@end
@interface Clown : Person
- (void) clownAround;
@end
@implementation Clown
- (void) clownAround
{
NSLog( @"how many clowns can come out of a Fiat 500?" );
}
@end
@interface HoboClown : Clown
- (void) fallDown;
@end
@implementation HoboClown
- (void) fallDown
{
NSLog( @"woops, the hobo clown fell down" );
}
@end
@interface Hobo : Person
- (void) hopATrain;
@end
@implementation Hobo
- (void) hopATrain
{
NSLog( @"eating beans on a train" );
}
@end
int main( void ){
NSAutoreleasePool *pool = [NSAutoreleasePool new];
id foo;
Person *p = [[Person alloc] init];
[p retain];
Clown *c = [[Clown alloc] init];
[c retain];
HoboClown *hc = [[HoboClown alloc] init];
[hc retain];
Hobo *h = [[Hobo alloc] init];
[h retain];
NSLog( @"Address: p = %p, c = %p", &p, &c );
NSLog( @"Points to: p = %p, c = %p", p, c );
[p doSomething];
[c clownAround];
[c release];
c = p;
[c retain];
[c doSomething];
NSLog( @"Address: p = %p, c = %p", &p, &c );
NSLog( @"Points to: p = %p, c = %p", p, c );
[hc fallDown];
// This will cause a crash
//[hc hopATrain];
foo = hc;
[foo retain];
[foo fallDown];
NSLog( @"Address: foo = %p, hc = %p", &foo, &hc );
NSLog( @"Points to: foo = %p, hc = %p", foo, hc );
// Is the class related to a class
if( [foo isKindOfClass: [Person class]] ){
NSLog( @"%@ is kind of %@", [foo description], [Person class] );
}else{
NSLog( @"%@ is not kind of %@", [foo description], [Person class] );
}
if( [foo isKindOfClass: [Hobo class]] ){
NSLog( @"%@ is kind of %@", [foo description], [Hobo class] );
}else{
NSLog( @"%@ is not kind of %@", [foo description], [Hobo class] );
}
// Check to see if the object is a particular class
if( [foo isMemberOfClass: [Person class]] ){
NSLog( @"%@ is member of %@", [foo description], [Person class] );
}else{
NSLog( @"%@ is not member of %@", [foo description], [Person class] );
}
if( [foo isMemberOfClass: [HoboClown class]] ){
NSLog( @"%@ is member of %@", [foo description], [HoboClown class] );
[(HoboClown*)foo fallDown];
}else{
NSLog( @"%@ is not member of %@", [foo description], [HoboClown class] );
}
if( [foo respondsToSelector: @selector(hopATrain) ] == NO ){
NSLog( @"%@ does not respond to selector %@", [foo description], @"hopATrain" );
}else{
NSLog( @"%@ does respond to selector %@", [foo description], @"hopATrain" );
[foo hopATrain];
}
if( [foo respondsToSelector: @selector(doSomething) ] == NO ){
NSLog( @"%@ does not respond to selector %@", [foo description], @"doSomething" );
}else{
NSLog( @"%@ does respond to selector %@", [foo description], @"doSomething" );
[foo doSomething];
SEL fallDownSelector = @selector( fallDown );
[foo performSelector: fallDownSelector ];
}
[foo release];
[p release];
[c release];
[hc release];
[h release];
[pool release];
return( 0 );
}