main函数:
#import <Foundation/Foundation.h>
#import "Person.h"
#import "GoodPerson.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
GoodPerson *g = [GoodPerson GoodpersonWithAge:20];
g.name = @"lisi";
g.sex = SexMan;
}
return 0;
}
Person.m
#import "Person.h"
@implementation Person
- (void)dealloc
{
NSLog(@"姓名是%@,年龄是%d,性别是%d,release",_name,_age,_sex);
[super dealloc];
}
+ (id)person
{
return [[[self alloc] init] autorelease];
}
+ (id)personWithAge:(int)age
{
Person *p1 = [self person];
p1.age = age;
return p1;
}
@end
GoodPerson.m
#import "GoodPerson.h"
@implementation GoodPerson
- (void)dealloc
{
NSLog(@"GOOD release");
[super dealloc];
}
+ (id)GoodpersonWithAge:(int)age
{
GoodPerson *g = [self person];
g.age = age;
return g;
}
end
问题1:为什么我明明没有创建Person对象,但是在对象销毁时,Person的对象却销毁了。
问题2:如果我将Person.m文件中 + (id)person函数改成如下:
+ (id)person
{
return [[[person alloc] init] autorelease];
}
程序运行不会报错,但是MJ老师视频中报错了?
谢谢各位大神的帮助
|