//person.M // -------- computer属性的存在时间 //------------ person的存在时间 #import "Person.h" #import "Computer.h" #import "ClassRoom.h" @implementation Person - (instancetype)init { self = [super init]; if (self) { NSLog(@"person init"); _computer = [[Computer alloc]init]; // _computer.keyword = self.delegate.keyword; _computer.delegate = self; } return self; } + (instancetype)personWithComputer:(Computer *)computer { Person *per = [[self alloc]init]; per.computer = computer; per.computer.delegate = per; // per.point.x = 5;// // [per autorelease];//在方法中加入autorelease,使其内存管制无误,有且仅有一个 return [per autorelease]; } - (void)walkToClassRoom:(ClassRoom *)classRoom { [classRoom.persons addObject:self]; } - (void)dealloc { [_computer release]; [super dealloc]; } @end
//person.h #import <Foundation/Foundation.h> @class Computer; @class ClassRoom; @interface Person : NSObject //{ // Computer *_computer; //} @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) Computer *computer; //strong: 在使用属性的过程中,保证对象存在 //retain: @property (strong, nonatomic) NSString *keyword; //建立一个便利构造,通过电脑对象初始化Person + (instancetype)personWithComputer:(Computer *)computer; - (void)walkToClassRoom:(ClassRoom *)classRoom; @end
//computer.m #import "Computer.h" @implementation Computer - (instancetype)init { self = [super init]; if (self) { NSLog(@"computer init"); } return self; } //循环引用 - (void)dealloc { self.delegate = nil;//研究知识点:当owner是strong属性和assign属性的时候,分别有什么作用 [super dealloc]; } @end
//person.h #import <Foundation/Foundation.h> #import "Person.h" @interface Computer : NSObject @property (assign, nonatomic)Person *delegate;//上一级 //assign: 弱引用,不增加引用计数,保证对象即时释放 //weak: @property (strong, nonatomic) NSString *keyword; @end
//----属性传值 //1.A.xx -> B.xx //2.A.xx -> C.xx // C.xx -> B.xx //----初始化/代理 方法传值 //----数据持久化(将任意数据归档之后,存放进本地文件) //关联式引用 //observer.m #import "Observer.h" #import "Computer.h" @implementation Observer - (instancetype)init { self = [super init]; if (self) { NSLog(@"observer init"); self.per = [[Person alloc]init]; self.per.keyword = self.keyword; // _per.computer.keyword = _per.keyword; _classRoom = [[ClassRoom alloc]init]; _classRoom.keyword = _per.keyword; [self actionBegin]; } return self; } - (void)actionBegin { [_per walkToClassRoom:_classRoom]; // [_per walkToClassRoom:_classRoom]; NSLog(@"persons = %@",_classRoom.persons); } @end
//observer.h #import <Foundation/Foundation.h> #import "Person.h" #import "ClassRoom.h" @interface Observer : NSObject @property (strong, nonatomic) Person *per; @property (strong, nonatomic) ClassRoom *classRoom; @property (strong, nonatomic) NSString *keyword; @end
#import "ClassRoom.h" @implementation ClassRoom - (instancetype)init { self = [super init]; if (self) { _persons = [NSMutableArray array]; } return self; } @end
#import <Foundation/Foundation.h> @interface ClassRoom : NSObject //给ClassRoom设置一个strong mutable的Array属性 @property (strong, nonatomic) NSMutableArray *persons;//先初始化后使用 @property (strong, nonatomic) NSString *keyword; @end
时间: 2024-10-16 00:51:05