Objective-c (inheritance, initialization method)

Source: Internet
Author: User

I. Inheritance

1. Inherited upper layer: parent class, inherited lower layer: subclass

2. Inheritance is one-way

3. Inheritance has transitivity: subclasses inherit the characteristics and behavior of the parent class

4. Subclass extension Parent class, more specific


Inheritance in OC

Inheritance in 1.oc, where one class inherits from another class;

2. The inherited class is called the parent class or superclass;

3. Inherited classes are subclasses


Characteristics of inheritance

Only single inheritance allowed in 1.oc

2. A class without a parent class is called the root class, and the root class in OC is NSObject

3. Inherited content: All instance variables and methods except private variables

4. Subclasses can override methods of the parent class


Super

Keywords in 1.oc

2. Role: Send a message to super, executable method implemented in the parent class


Two. Initialization method

-(ID) init

{

Send the INIT message to super; Execute the Init method implemented in the parent class

self = [super init];

if (self) {

Initialize settings

}

Returns the initialization completion object

return self;

}

Initialization process

1. Execute the method implemented in the parent class (recursively up to the initialization method in the NSObject class);

2. Start the initialization method from the root class nsobject;

3. Determine whether the initialization in the parent class is successful, that is, whether self exists

4. Complete the initialization settings of the object, return the object

Three. The construction of the toilet

1. Declaration and implementation of the convenience builder

+ (ID) personwithname: (NSString *) name

{

Person *p =[[person alloc]initwithname:name];

return p;

}

2. Creating objects with the convenience builder

Person *p =[Person personwithname:@ "Niaho"];




Objective-c (inheritance, initialization method)

Related Article

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.