object-c
- 格式:doc
- 大小:27.00 KB
- 文档页数:5
实验步骤:打开Xcode,创建文件,然后再打开文件,在编辑框进行编写代码。
创建一个Student的类,按照实验内容编写代码。
在main函数中实例化此学生对象,使用输出语句执行查看当前学生对象的retaincount的值;再使用方法 [student retain]; 然后,使用输出语句执行查看当前学生对象的retaincount的值;再次调用release方法使用[student release];使用输出语句执行来查看当前学生对象的retaincount的值,最后再次调用release方法使用[student release];使用输出语句执行来查看当前学生对象的retaincount的值;查看调用dealloc 方法。
调试试验代码,运行结果,分析实验结果。
1.实验代码:#import <Foundation/Foundation.h>@interface Student:NSObject{int number;}@end@implementation Student-(void)dealloc{NSLog(@"dead!");[super dealloc];}@endint main(int argc, const char * argv[]){@autoreleasepool {Student *student=[Student new];//[student retainCount];NSLog(@"%ld",[student retainCount]);[student retain];NSLog(@"%ld",[student retainCount]);[student release];NSLog(@"%ld",[student retainCount]);[student release];NSLog(@"%ld",[student retainCount]);}return 0;}实验结果:2013-11-27 16:31:04.693 yy[749:303] 12013-11-27 16:31:04.695 yy[749:303] 22013-11-27 16:31:04.695 yy[749:303] 12013-11-27 16:31:04.695 yy[749:303] dead!2013-11-27 16:31:04.696 yy[749:303] 1*//*2.实验代码:#import <Foundation/Foundation.h>#import <Foundation/NSArray.h>@interface Student:NSObject{int number;}@end@implementation Student-(void)dealloc{NSLog(@"dead!");[super dealloc];}@endint main(int argc,const char *argv[]){NSAutoreleasePool *pool=[NSAutoreleasePool new]; Student *student=[Student new];NSLog(@"%ld",[student retainCount]);NSMutableArray *array = nil;[array addObject:student];NSLog(@"%ld",[student retainCount]);[student release];NSLog(@"%ld",[student retainCount]);[array removeObject:student];NSLog(@"%ld",[student retainCount]);[pool drain];return 0;}/*实验结果:2013-11-27 16:47:37.100 yy[803:303] 12013-11-27 16:47:37.102 yy[803:303] 12013-11-27 16:47:37.102 yy[803:303] dead!2013-11-27 16:47:37.102 yy[803:303] 12013-11-27 16:47:37.103 yy[803:303] 1*/// 3.//实验代码:/*#import <Foundation/Foundation.h>@interface Computer:NSObject{}@end@implementation Computer-(void)dealloc{NSLog(@"dead!");[super dealloc];}@endint main(int argc,const char *argv[]){// NSAutoreleasePool *pool=[NSAutoreleasePool new]; Computer *apple=[[Computer alloc]init];@autoreleasepool {@autoreleasepool {[apple autorelease];[apple retain];NSLog(@"apple1 retaincount:%ld",[apple retainCount]);}NSLog(@"apple2 retaincount:%ld",[apple retainCount]);}NSLog(@"apple3 retaincount:%ld",[apple retainCount]);return 0;}实验结果:2013-11-27 16:56:59.035 yy[820:303] apple1 retaincount:22013-11-27 16:56:59.037 yy[820:303] apple2 retaincount:12013-11-27 16:56:59.042 yy[820:303] apple3 retaincount:1*//*//实验代码:#import <Foundation/Foundation.h>@interface Computer : NSObject{}@end@implementation Computer-(void)dealloc{NSLog(@"dead!");[super dealloc];}@endint main(int argc,const char *argv[]){// NSAutoreleasePool *pool=[NSAutoreleasePool new];Computer *apple=[[Computer alloc]init];@autoreleasepool {[apple autorelease];@autoreleasepool {[apple retain];NSLog(@"apple1 retaincount:%ld",[apple retainCount]); }NSLog(@"apple2 retaincount:%ld",[apple retainCount]);}NSLog(@"apple3 retaincount:%ld",[apple retainCount]);return 0;}实验结果:2013-11-27 17:36:50.103 yy[922:303] apple1 retaincount:22013-11-27 17:36:50.105 yy[922:303] apple2 retaincount:22013-11-27 17:36:50.105 yy[922:303] apple3 retaincount:1*///实验小结:创建一个对象时,引用计数值为1,输出的值为1,使用方法[student retain]; 引用计数的值为2,再使用方法[student release];实行减1操作,引用计数的值为1,再使用方法[student release];引用计数的值为0,则调用重载dealloc 方法。
最后,动态释放池没有释放,此时的引用计数为1。
输出的实验结果为1。
创建一个对象时,引用计数值为1。
将apple对象添加到第二个动态释放池中。
使用方法 [apple autorelease];使用方法[student retain];引用计数的值为2,输出的结果为2.到动态释放池结束。
引用计数的值为1.而apple对象没添加到第一个动态释放池,因此引用计数的值为1.当第一个动态释放池结束,引用计数的值仍不变。
创建一个对象时,引用计数值为1。
将apple对象添加到第一个动态释放池中。
使用方法 [apple autorelease];在第二个动态释放池,使用方法[student retain];引用计数的值为2,输出的结果为2.到动态释放池结束。
引用计数的值为2.而apple对象没添加到第二个动态释放池,因此引用计数的值为2.输出的结果为2。
当第一个动态释放池结束,引用计数的值变为1。
最后,输出的结果为1.。