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,如需转载请自行联系原作者