观察下面这段代码和以前代码的区别,主要是看self ; 一般self 是代表当前 对象。
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
1.#import
2.
3.@interface Animal: NSObject
4.
5.@property int Speed, Legs;
6.
7.-(void) display;
8.
9.-(void) set_speed:(int) speed o:(int) legs ;
10.
11.@end
12.
13.@implementation Animal
14.
15.@synthesize Speed,Legs;
16.
17.-(void) display
18.{
19. NSLog(@"run speed %i",Speed);
20. NSLog(@"I have %i legs",Legs);
21.}
22.
23.-(void) set_speed:(int) speed o: (int) legs
24.{
25. Speed = speed;
26. Legs = legs;
27. [self display];
28.}
29.@end
30.
31.
32.int main(int argc, const char * argv[])
33.{
34.
35. @autoreleasepool {
36. Animal *dog;
37.
38. dog = [Animal alloc];
39. dog = [dog init];
40.
41.
42. [dog set_speed:30 o: 4];
43.
44.
45. Animal * rabbit = [[Animal alloc] init];
46.
47. [rabbit set_speed:56 o:4];
48.
49.
50. // insert code here...
51. NSLog(@"Hello, World!");
52.
53. }
54. return 0;
55.}
阅读(1906) | 评论(0) | 转发(0) |