Inheritance, initialization, method, convenience builder

Source: Internet
Author: User

Inheritance Features

only single inheritance is allowed in OC .

The class of the parent class is called the root class, and the root class in OC is nsobject( ancestor ).

Inherited content: All instance variables and? methods.

If the subclass is not satisfied? The implementation of the parent class? Method can be overridden (overwrite)? Method of the parent class .

Inheritance has transitivity

Completion of initialization method

-(void) init

{

Send the INIT message to Super: Executes the Init method implemented in the parent class

self = [super init];

if (self)

{

Initialize settings

}

Returns the object that was initialized for completion

return self;

}

The above is called the specified initialization method

Specifies that the initialization method has more than one other initialization method with multiple

Convenience Builder (+ method)

Returns an instance of this type, with the method name beginning with the class name.

Internal implementation: Encapsulates the Alloc and initialization methods. It's more concise.

1. Declaration and implementation of the convenience builder

+ (person *) Personwithname: (nssting *) name

{

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

return p;

}

2. Create objects using the convenience builder

Person *p = [Person personwithname:@ "ianhao.cn"];

Inheritance, initialization, method, convenience builder

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.