1
1.dog.speed = 30;
和下面的方法效果相同。
1
1.[dog setSpeed: 30];
函数多参数的几种用法
使用 参数名,例如:'over',这里 'over'可以换成其他的单词。
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
1.#import
2.
3.@interface Animal: NSObject
4.
5.@property int Speed, Legs;
6.
7.-(void) display;
8.
9.-(void) set_speed:(int) speed over:(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 over: (int) legs
24.{
25. Speed = speed;
26. Legs = legs;
27.}
28.@end
29.
30.
31.int main(int argc, const char * argv[])
32.{
33.
34. @autoreleasepool {
35. Animal *dog;
36.
37. dog = [Animal alloc];
38. dog = [dog init];
39.
40.
41. [dog set_speed:30 over: 4];
42. [dog display];
43. // insert code here...
44. NSLog(@"Hello, World!");
45.
46. }
47. return 0;
48.}
不使用 参数名:
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
1.#import
2.
3.@interface Animal: NSObject
4.
5.@property int Speed, Legs;
6.
7.-(void) display;
8.
9.-(void) set_speed:(int) speed :(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 : (int) legs
24.{
25. Speed = speed;
26. Legs = legs;
27.}
28.@end
29.
30.
31.int main(int argc, const char * argv[])
32.{
33.
34. @autoreleasepool {
35. Animal *dog;
36.
37. dog = [Animal alloc];
38. dog = [dog init];
39.
40.
41. [dog set_speed:30 : 4];
42. [dog display];
43. // insert code here...
44. NSLog(@"Hello, World!");
45.
46. }
47. return 0;
48.}
阅读(1998) | 评论(0) | 转发(0) |