【要求】创建一个学生对象,要求快速创建,并初始化年龄为指定的值
例如:在student类实现中,自定义以下两个方法: - //初始化年龄
- -(instancetype)initWithAge:(int)age{
- if (self = [super init]) {
- self.age = age;
- }
- return self;
- }
-
- +(instancetype)studentWithAge:(int) age{
- //快速创建对象,并管理内存
- return [[[Student alloc] initWithAge:age]autorelease];
- }
- -(void)dealloc{
-
- NSLog(@"This Student dead....");
- [super dealloc];
- }
复制代码
在main.m文件中 - @autoreleasepool {
- Student *st = [Student studentWithAge:24];
- NSLog(@"student age = %d",st.age);
- }
复制代码
打印结果: 2015-10-06 21:51:18.087 MRCDemo[3265:303] student age= 24 2015-10-06 21:51:18.089 MRCDemo[3265:303] This Studentdead.... 2015-10-06 21:51:18.090 MRCDemo[3265:303] This persondead....
|