objective-c 基礎語法和習題 / 類 詳解/合成存取

1.@interface Animal: NSObject
2.
3.@property int speed;
4.
5.-(void) run_speed;
6.-(void) set_speed:(int) n;
7.
8.@end
9.
10.@implementation Animal
11.
12.@synthesize speed;
13.
14.-(void) run_speed
15.{
16. NSLog(@"run speed %i",speed);
17.}
18.-(void) set_speed:(int) n
19.{
20. speed = n;
21.}
22.@end

在 @interface 增加 @property,在 @implementation 裏寫上 @synthesize

編譯器會默認的添加 setSpeed函數

下面語句會成功。

1.       
2.        [dog setSpeed: 30];
3.        [dog run_speed];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章