OC development-custom construction method (29)

I. Overview

  • In the class initialized by the system default construction method, the value of the variable is the same (uninitialized to 0, fixed value after initialization)
  • Through the custom construction method, set the variable value during initialization, so that the value of the constructed class variable is not the same

Two init initialization process

2.1 Schematic

2.2 Description

  • Inheritance: NSObject is the parent class, Person inherits NSObject, Student inherits Student
  • When init is initialized, Stuent's init will call Person's init, Person's init will call NSObject's initialization

Three custom construction methods

3.1 Person

//Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *name;
@property int age;

-(instancetype)initWithName:(NSString*)name;
-(instancetype)initWithAge:(int)age;
-(instancetype)initWithName:(NSString*)name andAge:(int)age;
@end

//Person.m

#import "Person.h"
@implementation Person
-(instancetype)init
{
    if(self=[super init])
    {
        _name=@"jack";
    }
    return self;
}
- (instancetype)initWithName:(NSString *)name
{
    if(self=[super init])
    {
        _name=name;
    }
    return self;
}
- (instancetype)initWithAge:(int)age
{
    if(self=[super init])
    {
        _age=age;
    }
    return self;
}
- (instancetype)initWithName:(NSString *)name andAge:(int)age
{
    if(self=[super init])
    {
        _name=name;
        _age=age;
    }
    return self;
}
@end

3.2 Student

//Student.h
#import "Person.h"
@interface Student : Person
@property int no;
-(instancetype)initWithNo:(int)no;
-(instancetype)initWithName:(NSString*)name andAge:(int)age andNo:(int)no;
@end

//Student.m
#import "Student.h"

@implementation Student

- (instancetype)initWithNo:(int)no
{
    if(self=[super init])
    {
        _no=no;
    }
    return self;
}
//- (instancetype)initWithName:(NSString *)name andAge :(int)age andNo:(int)no
//{
//    if(self=[super init])
//    {
//        self.name=name;
//        self.age=age;
//        _no=no;
//    }
//    return self;
//}
- (instancetype)initWithName:(NSString *)name andAge:(int)age andNo:(int)no
{
    if(self=[super initWithName:name andAge:age])
    {
        _no=no;
    }
    return self;
}
@end

Four constructor call

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        Person *p=[[Person alloc]init];
        //NSLog(@"Person 的名字是%@",p.name);
        
        //使用自定义name
        Person *p1=[[Person alloc] initWithName:@"Rose"];
        //NSLog(@"Person的名字是%@",p1.name);
        
        Person *p2=[[Person alloc]initWithAge:10];
        //NSLog(@"Person的年龄是%d",p2.age);
        
        Person *p3=[[Person alloc]initWithName:@"jack" andAge:10];
        //NSLog(@"Person的名字是%@,年龄是%d",p3.name,p3.age);
        
        Student *stu=[[Student alloc]initWithNo:1];
        //NSLog(@"Student的编号是%d",stu.no);
        
        Student *stu2=[[Student alloc]initWithName:@"jack" andAge:10 andNo:1];
        NSLog(@"Student的名字是%@,年龄是%d,编号是%d",stu2.name,stu2.age,stu2.no);
           
    }
    return 0;
}
Published 362 original articles · 118 praises · 530,000 views

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/105422849