OC 方法声明使用

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    int _age;
}

- (void)setAge:(int)age; //法名是setAge:
- (int)age; // 方法名是age

// 方法名是setAge:andNo:
// - (void)setAge:(int)newAge andNo:(int)no;
@end

Person.m

#import "Person.h"

@implementation Person

- (void)setAge:(int)age {
    NSLog(@"调用了setAge方法:%i", age);
    _age = age;
    
    // 这是错误的写法,会导致死循环,无限调用set方法
    // self.age = newAge;// [self setAge:newAge];
}

- (int)age {
    NSLog(@"调用了age方法:%i", _age);
    
    return _age;
}
@end
原文地址:https://www.cnblogs.com/liuwj/p/6899581.html