Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220029
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1506
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-25 10:52
文章分类
文章存档

2014年(22)

2013年(31)

我的朋友

分类: 网络与安全

2014-03-04 15:21:10

观察下面这段代码和以前代码的区别,主要是看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.}
阅读(1811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~