您的位置:首页 > 其它

内存管理4Aotorelease自动引用计数

2013-08-02 15:11 411 查看
Student.h:

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property(nonatomic,unsigned)int age;
+(id)student;
+(id)initWithAge:(int)age;
@end

Student.m:
#import "Student.h"

@implementation Student

+(id)student{
return [[[Student alloc] init] autorelease];
}

+(id)initWithAge:(int)age{
Student *stu=[[[Student alloc] init] autorelease];
stu.age=age;
return stu;
}

-(void)dealloc{
NSLog(@"%@被销毁",self);
[super dealloc];
}
@end

main:
#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[])
{
//@autoreleasepool代表自动创建一个自动释放池
@autoreleasepool {

Student * stu=[[Student alloc] init];
[stu autorelease];

Student * stu1=[[Student alloc] init];
[stu1 autorelease];

//快速创建Student对象
Student * stu3=[[Student student] autorelease];
}
// @autoreleasepool {
//
// Student * stu2=[[[Student alloc] init] autorelease];
//// [stu2 autorelease];
//
// }
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐