Details (constructor)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.