#import <Foundation/Foundation.h>
#import "User.h"
/*
1.文件管理器
简单的单例模式
*/
int main(int argc, const char * argv[])
{
//文件管理器
//一般不会使用
// NSFileManager * fileManage =
// [[NSFileManager alloc]init];
NSFileManager * fm = [NSFileManager defaultManager];
NSLog(@"%p",fm);
//创建一个单例对象
//单例对象的生命周期是从函数開始到函数结束
NSFileManager * fm1 = [NSFileManager defaultManager];
NSLog(@"%p",fm1);
User * user1 = [User defaultUser];
NSLog(@"%p",user1);
user1.age = 10;
NSLog(@"%p age:%d",user1,user1.age);
User * user2 = [User defaultUser];
NSLog(@"%p age:%d",user2,user2.age);
NSLog(@"%p",user2);
return 0;
}
#import <Foundation/Foundation.h>
@interface User : NSObject
{
int _age;
}
-(void)setAge:(int)age;
-(int)age;
+(User *)defaultUser;
@end
#import "User.h"
static User * instance = nil;
@implementation User
-(void)setAge:(int)age
{
_age = age;
}
-(int)age
{
return _age;
}
+(User *)defaultUser
{
//推断对象是否为空
if (instance == nil) {
//为空进行初始化
instance =[[self alloc]init];
}
return instance;
}
@end