Objective-c (5) Inheritance and polymorphism

Source: Internet
Author: User

    • Inheritance relationships for classes

Compound relationship: Has a

Inheritance relationship: is a

Consider this question:

When you write cat and dog classes, you'll find that there are a lot of the same or similar properties and methods in these two classes.

The properties and methods common to animals are placed in an animal class, and the dog and cat classes inherit the Automatic object class.

Dogs and cats, respectively, to achieve their own unique properties and methods


b Inherits A = = a derives b

A is called a parent class or base class, and B is called a subclass or derived class

b contains all properties and methods of a


The inheritance relationship is specified in the interface section:

@interface Classname:parentclassname

...

@end

NSObject is the base class for all OC classes and is referred to as "super class"


Only one---single inheritance can be inherited

Some languages, such as C + +, can inherit multiple, that is, a class can have more than one parent class, and OC does not support multiple inheritance

C + + supports multiple designations of inheritance, that is, when inheriting, you can specify public, protected, private to determine the access rights of members in the parent class in the subclass, and OC does not support multiple inheritance methods.

Although OC does not support multiple inheritance, the use of "protocol" in OC implements a subset of the features of multiple inheritance.

Other Notes:

No child classes are allowed to have the same name as the parent class in OC

When a subclass calls a method, it is found in the current class, not found in the parent class.

Subclasses can override methods of the parent class, that is, methods that override the parent class

The @protected property in the parent class can be accessed in the subclass method, but not @private.


How Inheritance works:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7C/7E/wKiom1bRiwjQQHO7AACRwWguBkI961.png "title=" screen shot 2016-02-27 pm 7.39.11.png "width=" height= "179" border= "0" hspace= "0" vspace= "0" style= "WIDTH:500PX;HEIGHT:179PX;" alt= "Wkiom1briwjqqho7aacrwwgubki961.png"/>

ISA is a member variable in a superclass nsobject that points to a class object of that type


In the method of the object, the self pointer points to the current object

You can access the properties of an object and invoke methods through the self pointer

In the method of the object, the super pointer points to the parent class part of the current object

The super pointer provides access to the properties of the parent class and to the calling method (especially the overridden method)


    • Re-parent class method

Subclasses contain all the properties and methods of the parent class, where the parent class's methods can be overridden in subclasses.

Rewrite requirements: The method name, parameter, and return value are consistent with the parent class method.

For example: Overriding the Description method enables the output of the specified string when nslog to%@ output.

When overriding a parent class method, you sometimes need to perform the action of the parent class, using the super pointer to specify the method that invokes the parent class.


    • Polymorphic

What happens when a method is called when the parent pointer stores the child class object address

Different actions that are caused by the same message to different objects


The actual method of the object is detected when the object method is called

The polymorphism of the method has been implemented in OC


Note: It is unsafe to have a subclass pointer pointing to the parent class object


Benefits of Polymorphism:

Replace many copies of code with one copy of the code (such as a function passing a base-class pointer)


Limitations of polymorphism:

A pointer variable of a parent class type is a method that cannot be used to invoke a subclass (sometimes although it succeeds but is not recommended)

If necessary, you should convert the parent class pointer to a child class pointer before calling a method of the subclass


    • Multi-state pointer

The parent object pointer can store the address of the subclass object, such that the pointer is called a polymorphic pointer

Polymorphic pointers are often used as parameters, return values, etc.

The ID is the most commonly used polymorphic pointer type in OC, equivalent to nsobject*

Another type, instancetype, can also be considered an ID type

But only on the return value of some methods, a type check function is more than the ID type.

When you assign a null value to an object pointer, you should use nil, and null should be used when assigning a value to a non-OC object pointer.





This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745611

Objective-c (5) Inheritance and polymorphism

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.