Day9 oc constructor, destructor and class essence

oc constructor

//The new method does the following two things. Generally, new is rarely used in development, because the allocation of space cannot be initialized in other ways.
Person *p = [Person new];
//1. Call +alloc to allocate storage space
Person *p1 = [Person alloc];
//2, call -init to initialize
Person p2 = [p1 init];
//Person *p3 = [[Person alloc] init]; write together

 The constructor is the method used to initialize the object, so the init method is the constructor. The member variable value is 0 after initialization.

//Rewrite the -init method, you can assign values ​​to member variables, because the return type of the subclass is uncertain (polymorphism), so write the id type, id is equivalent to NSObject * universal pointer
-(id)init
{
//Be sure to call super's init method first to initialize the member variables of the parent class, such as isa (the class that points to the object) and attributes
//[super init]; current object self
if(self=[super init])//Assign to self first, if self is not empty, execute the following
{
//Initialization successful do something... Initialize internal member variables of subclasses
}
return self;
}

 custom constructor

1. Must be an object method, starting with -

2. The return value is generally of type id

3. The method name generally starts with init

4. The attributes of the parent class call the parent class method to process, such as [super initwithxxx], the subclass handles its own

-(id)initWithName:(NSString *)name andAge:(int)age
{
if(self=[super init])
{
_name = name;
_age = age;
}
return self;
}

destructor

When the object counter is decremented to 0, dealloc is called automatically

-(void)dealloc
{
do something...
[super dealloc];//Be sure to call dealloc at the end
}

 

 

The essence of oc class

The object created by a class is an instance object. In fact, the class itself is also an object, which is an object of class type, referred to as a class object.

typedef struct objc_class *Class;

Person *p1 = [[Person alloc] init];
Person *p2 = [[Person alloc] init];

//Get the class object in memory
Class c1 = [p1 class];
Class c2 = [p2 class];
[c1 test];//Call the class method of the class, which is equivalent to [Person test]
//or
Class c3 = [Person class];

/ / Use class to create a Person class object
/ / Use the Person class object to create an object of type Person

 Class object == class name (c3==Person), a class has only one class object

Class loading and initialization

1. When the program starts, all classes and categories in the project will be loaded once, and only once. Load the parent class first and then load the child class. Class loading calls class and class + load.

+(void)load

{

}

2. When this class is used for the first time, the +initialize method of the current class will be called. First initialize the parent class and then initialize the child class. You can monitor when a class is used.

+(void)initialize

{

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326968883&siteId=291194637