Objective-C Inheritance Mechanism

Source: Internet
Author: User

1. Inheritance

1. Basic Concepts

A new class can be derived from an existing class. This process is called inheritance. A new class is called a subclass, and an existing class is called a parent class or a superclass.

2. inheritance features:

ImplementationCodeReuse: subclass can reuse the instance variables and methods of the parent class.

Subclass can add strength variables and methods to expand the parent class, but cannot overload the instance variables of the parent class

All types have a public base class: nsobject

3. nsobject class

You can define your own root class, but do not normally use the existing class.

Nsobject provides reference counting, memory allocation, runtime type identification, and other information. You only need to declare a custom type without nsobject subclass to reuse these mechanisms.

For example, you can use [Super init] In the subclass to call the initialization method of the parent class. [Superdealloc] Call the memory release method of the parent class.

4. Syntax format:

@ Interfacemyclass: nsobject

...

@ End

Only public inheritance and single inheritance are supported.

5. Subclass inherits all methods of the parent class.If the subclass has the same name as the parent class, The number, type, and return value of the parameter are the same, then the subclass method willOverwriteThe parent class method is equivalent to a virtual function in C ++ to overwrite the override.

6. The nsobject class implements the alloc, delloc, release, and init methods.

Alloc does not need to be overloaded in the subclass.

Dealloc releases the memory applied during initialization, which may be overloaded.

Release makes the object reference count-1. When the object reference count is 0, the system automatically calls the object's delloc method to release the object's memory. This method generally does not need to be overloaded.

Init usually needs to be reloaded to initialize the object.

The general format of the subclass init method is as follows:

-(ID) Init

{

If (Self = [Super init])

{

... // Initialize subclass instance variables

}

Return self;

}

 

Ii. inherited message transmission

Inherit the message passing rule: This class first and then the parent class. If no corresponding message is found in the root class, an error is returned.

 

3. Combination

1. concept: If the instance variable of a class is the object of another class, the two classes are "hasa" relationships, which express the overall-partial relationship, however, in objective-C, such instance variablesObject Pointer onlyCannot be an object. This method is called combination.

2.Note: In the combination mode, the object pointer is used as the init and set methods of the instance variables. Generally, the dealloc method is rewritten to release the memory. Be sure to follow the memory management mechanism to prevent memory leakage.

For details about memory management, see 《Objective-CMemory Management Mechanism.

 

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.