//
// main.m
// 匿名分类(延展)
// 可以为某个类扩展私有的成员变量和方法,写在.m文件中,
// 分类不可以扩展属性,分类有名字,匿名分类没有名字。
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person *p = [Person new];
p->_age; //报错
[p say]; //报错
return 0;
}
//
// Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
//{
// int _age;
//}
//- (void)eat;
//- (void)say;
@end
//
// Person.m
#import "Person.h"
@interface Person () //没有名字,所以叫匿名分类,不需要实现,
{
int _age; //私有的属性
}
- (void)say; //私有的方法
@end
@implementation Person
//{
// int _age; //私有的属性
//}
-(void)eat //没有声明只有实现,方法是私有的,
{
NSLog(@"%s", __func__);
}
- (void)say //没有声明只有实现,方法是私有的,
{
NSLog(@"age = %i", _age);
}
@end