您的位置:首页 > 移动开发 > Objective-C

objective-c 基础语法和习题 / self 的用法

2014-03-12 11:51 260 查看
观察下面这段代码和以前代码的区别,主要是看self ; 一般self 是代表当前 对象。
1.#import <Foundation/Foundation.h>
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.}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息