请教一下,用类方法创建自定义对象和用init创建同样功能的自定义对对象,有什么区别呢?
部分代码如下:
+ (id)studentWithName:(NSString *)name andAge:(int)age andNo:(int)no andBook:(Book *)book
{/*
Student *st = [[[self alloc] init] autorelease];
st.name = name;
st.age = age;
st.no = no;
st.book = book;
*/
Student *st = [self personWithName:name andAge:age];
st.no = no;
st.book = book;
return st;
}
- (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no andBook:(Book *)book
{
if (self = [super init])
{
self.name = name;
self.age = age;
self.no = no;
self.book = book;
}
return self;
} |
|