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

本文介绍了一个使用Objective-C编写的简单学生类实现。该类包括年龄属性,并通过类方法进行对象初始化。文中提供了完整的源代码,包括头文件(.h)和实现文件(.m),展示了如何在main函数中创建并使用此类。

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; }























本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366774,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值