父类中
- (id)initWithName:(NSString *)aName
sex:(NSString *)aSex
age:(NSInteger)aAge
{
//调用父类的初始化方法
self = [super init];//防止初始化失败,赋值失败
if (self) {
//给自己的实例变量赋值
_name = aName;
_sex = aSex;
_age = aAge;
}
return self;
}
sex:(NSString *)aSex
age:(NSInteger)aAge
{
//调用父类的初始化方法
self = [super init];//防止初始化失败,赋值失败
if (self) {
//给自己的实例变量赋值
_name = aName;
_sex = aSex;
_age = aAge;
}
return self;
}
子类中(指定)
- (id)initWithName:(NSString *)aNamescore:(float)aScore
sex:(NSString *)aSex
age:(NSInteger)aAge
number:(NSString *)aNumber
{
//super 仅仅有一个作用,调用父类方法
//调用父类方法,给父类的实例变量赋值
self = [super initWithName:aName sex:aSex age:aAge];
if (self) {
//给自己的实例变量赋值
_number = aNumber;
_score = aScore;
}
return self;
}
其它初始化方法(一般)
- (id)initWithName:(NSString *)aNamescore:(float)aScore
number:(NSString *)aNumber
{
return [self initWithName:aName score:aScore sex:Nilage:0number:aNumber];
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。