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];