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

 

相关文章:

  • 2022-01-04
  • 2022-02-25
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
猜你喜欢
  • 2022-03-02
  • 2021-12-27
  • 2022-12-23
  • 2021-08-30
  • 2021-08-22
  • 2021-06-18
  • 2021-08-03
相关资源
相似解决方案