写一个Person类,要保证无论什么情况下创建Person对象返回的都是同一个对象(单例设计模式)
@interface Person : NSObject
+ (Person *)sharedManager;
@property (nonatomic,retain) NSString *name;
@end
//Person类的实现
@implementation Person
//定义一个全局变量,这句不能掉。
static Person *per =nil;
+ (Person *)sharedManager
{
if(per == nil){
per = [Person new];
}
return per;
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person *person1 = [Person sharedManager];
Person1.name = @"lixue";
Person *person2 = [Person sharedManager];
NSLog(@"%p", person1);
NSLog(@"%p", person2);
return 0;
} |
|