便利构造器
遍历构造器封装了对象创建的过程:
内部实现:封装了alloc和初始化操作,创建对象更加方便快捷.
.h文件中:
.m文件中:
-(id)initWithName:(NSString*)name sex:(NSString*)sex age:(NSInteger)age school:(NSString*)school number:(NSInteger)number major:(NSString*)major academy:(NSString*)academy
{
self= [super initWithName:name sex:sex age:age school:school number:number];
if(self) {
_major= major;
_academy= academy;
}
returnself;
}
//实现便利构造器
+(id)collegeStudentWithName:(NSString*)name sex:(NSString*)sex age:(NSInteger)age school:(NSString*)school number:(NSInteger)number major:(NSString*)major academy:(NSString*)academy
{
CollegeStudent*student = [[CollegeStudent alloc]initWithName:name sex:sexage:ageschool:school number:number major:major academy:academy];
return student;
}
方法实现:
//创建一个大学生
CollegeStudent *student1=[CollegeStudent collegeStudentWithName:@"张三" sex:@"男" age:18 school:@"�清华大学" number:123123123 major:@"�信息学院" academy:@"�电子信息工程技术"];