#import <Foundation/Foundation.h> /* self可以在方法、set、get中使用。 */ @interface Person : NSObject { int _age; } - (void)setAge:(int)age; - (int)age; - (void)test; @end @implementation Person - (void)setAge:(int)age { // _age = age; self->_age = age; } - (int)age { return self->_age; } - (void)test { // self:指向了方向调用者,代表着当期对象 int _age = 20; NSLog(@"Person的年龄是%d岁", self->_age); } @end int main() { Person *p = [Person new]; [p setAge:10]; [p test]; return 0; }
灰摩托(huimotuo)http://www.cnblogs.com/huimotuo/