Constructor: the method used to initialize an object. It is an object method. The purpose of the constructor is to rewrite the constructor at the beginning. To create an object, the member variable has fixed values.
1. Person * P = [person new]; in practical applications, new is rarely used, because new is rigid.
1> Create A Complete Available object
* Bucket allocation + alloc person * P1 = [person alloc];
* Initialize-init person * P2 = [P1 init];
* The final combination is person * P = [[person alloc] init];
2. Rewrite-init Constructor
1> be sure to call back the super init method: Initialize some member variables and attributes declared in the parent class * Self = [Super init]; // The current object self
2> if the object is initialized successfully, it is necessary to perform subsequent initialization.
If (self! = Nil)
{// Initialization successful
_ Age = 10;
}
Return self;
3> return an object that has been initialized
4> simplified to get our standard initialization method:
-(ID) Init
{
If (Self = [Super init])
{
_ Age = 10;
}
Return self;
}
Details (constructor)