TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
Person.h- //
- // Person.h
- // memory
- //
- // Created by yusian on 14-1-11.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- @interface Person : NSObject
- {
- Dog * _dog;
- }
- - (void) setDog:(Dog *)aDog;
- - (Dog *) dog;
- @end
复制代码 Person.m- //
- // Person.m
- // memory
- //
- // Created by yusian on 14-1-11.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "Person.h"
- @implementation Person
- - (Dog *) dog{
- return _dog;
- }
- - (void) setDog:(Dog *)aDog{
- if (aDog != _dog) {
- [_dog release];
- _dog = [aDog retain];
- }
- }
- - (void) dealloc{
- NSLog(@"Person is dealloc!");
- [_dog release];
- [super dealloc];
- }
- @end
复制代码 Dog.h- //
- // Dog.h
- // memory
- //
- // Created by yusian on 14-1-11.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Dog : NSObject
- {
- int _ID;
- }
- @property int ID;
- @end
复制代码 Dog.m- //
- // Dog.m
- // memory
- //
- // Created by yusian on 14-1-11.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "Dog.h"
- @implementation Dog
- @synthesize ID = _ID;
- - (void) dealloc{
- NSLog(@"Dog %d is dealloc!",_ID);
- [super dealloc];
- }
- @end
复制代码 main.m- //
- // main.m
- // memory
- //
- // Created by yusian on 14-1-11.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- #import "Person.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
-
- Dog * dog1 = [[Dog alloc] init];
-
- Dog * dog2 = [[Dog alloc] init];
-
- Person * sim = [[Person alloc] init];
-
- Person * sian = [[Person alloc] init];
-
- [dog1 setID:1];
-
- [dog2 setID:2];
-
- NSLog(@"//初始化两条狗");
- NSLog(@"dog %d retainCount1 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount1 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog1];//sim开始遛狗dog1
-
- NSLog(@"//sim开始遛狗dog1");
- NSLog(@"dog %d retainCount2 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount2 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sian setDog:dog1];//sian开始遛狗dog1
-
- NSLog(@"//sian开始遛狗dog1");
- NSLog(@"dog %d retainCount3 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount3 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog2];//sim开始遛狗dog2
-
- NSLog(@"//sim开始遛狗dog2");
- NSLog(@"dog %d retainCount4 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount4 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sian setDog:dog2];//sian开始遛狗dog2
-
- NSLog(@"//sian开始遛狗dog2");
- NSLog(@"dog %d retainCount5 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount5 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim release];//sim离开
-
- NSLog(@"//sim离开");
- NSLog(@"dog %d retainCount6 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount6 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sian release];//sian离开
-
- NSLog(@"//sian离开");
- NSLog(@"dog %d retainCount7 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount7 is %ld",[dog2 ID],[dog2 retainCount]);
-
- NSLog(@"//dog1 and dog2 is release");
- [dog1 release];
- [dog2 release];
-
-
- }
- return 0;
- }
复制代码 输出结果:- 2014-01-11 23:08:52.260 memory[834:303] //初始化两条狗
- 2014-01-11 23:08:52.261 memory[834:303] dog 1 retainCount1 is 1
- 2014-01-11 23:08:52.261 memory[834:303] dog 2 retainCount1 is 1
- 2014-01-11 23:08:52.262 memory[834:303] //sim开始遛狗dog1
- 2014-01-11 23:08:52.262 memory[834:303] dog 1 retainCount2 is 2
- 2014-01-11 23:08:52.262 memory[834:303] dog 2 retainCount2 is 1
- 2014-01-11 23:08:52.263 memory[834:303] //sian开始遛狗dog1
- 2014-01-11 23:08:52.263 memory[834:303] dog 1 retainCount3 is 3
- 2014-01-11 23:08:52.263 memory[834:303] dog 2 retainCount3 is 1
- 2014-01-11 23:08:52.264 memory[834:303] //sim开始遛狗dog2
- 2014-01-11 23:08:52.264 memory[834:303] dog 1 retainCount4 is 2
- 2014-01-11 23:08:52.264 memory[834:303] dog 2 retainCount4 is 2
- 2014-01-11 23:08:52.265 memory[834:303] //sian开始遛狗dog2
- 2014-01-11 23:08:52.265 memory[834:303] dog 1 retainCount5 is 1
- 2014-01-11 23:08:52.265 memory[834:303] dog 2 retainCount5 is 3
- 2014-01-11 23:08:52.266 memory[834:303] Person is dealloc!
- 2014-01-11 23:08:52.266 memory[834:303] //sim离开
- 2014-01-11 23:08:52.266 memory[834:303] dog 1 retainCount6 is 1
- 2014-01-11 23:08:52.267 memory[834:303] dog 2 retainCount6 is 2
- 2014-01-11 23:08:52.267 memory[834:303] Person is dealloc!
- 2014-01-11 23:08:52.267 memory[834:303] //sian离开
- 2014-01-11 23:08:52.268 memory[834:303] dog 1 retainCount7 is 1
- 2014-01-11 23:08:52.268 memory[834:303] dog 2 retainCount7 is 1
- 2014-01-11 23:08:52.268 memory[834:303] //dog1 and dog2 is release
- 2014-01-11 23:08:52.269 memory[834:303] Dog 1 is dealloc!
- 2014-01-11 23:08:52.269 memory[834:303] Dog 2 is dealloc!
- Program ended with exit code: 0
复制代码
|
|