美文网首页
便利构造器

便利构造器

作者: 叶语JM | 来源:发表于2016-04-16 22:10 被阅读42次

    遍历构造器封装了对象创建的过程:

    内部实现:封装了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:@"�电子信息工程技术"];

    相关文章

      网友评论

          本文标题:便利构造器

          本文链接:https://www.haomeiwen.com/subject/lnkolttx.html