点语法本质
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
Person *p = [Person new];
// 点语法的本质还是方法调用
p.age = 10; // [p setAge:10];
int a = p.age; // [p age];
p.name = @"Jack";
NSString *s = p.name;
NSLog(@"%@", s);
return 0;
}
@interface Person : NSObject
{
int _age;
NSString *_name;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setName:(NSString *)name;
- (NSString *)name;
@end
#import "Person.h"
@implementation Person
- (void)setAge:(int)age
{
//_age = age;
NSLog(@"setAge:");
// 会引发死循环
//self.age = age; // [self setAge:age];
}
- (int)age
{
NSLog(@"age");
return _age;
// 会引发死循环
//return self.age;// [self age];
}
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
@end
时间: 2024-10-10 17:45:42