Objective-C basics for iOS development-Polymorphism

Source: Internet
Author: User

Objective-C language is an advanced object-oriented programming language. Therefore, it has some characteristics of object-oriented programming, namely encapsulation, inheritance and polymorphism.

Today we will introduce polymorphism in objective-C.

 

1. What is polymorphism?

 

Polymorphism: the ability of different objects to respond to the same message in their own way is called polymorphism.

Because each class belongs to the namespace of the class, polymorphism is called possible. The names in the class definition do not conflict with those outside the class definition. Class instance variables and class methods have the following features:

  • Similar to the data member in the structure in C, the instance variable of the class is also located in the namespace exclusive to the class.

  • The class method is also located in the namespace exclusive to the class. Unlike the method name in C, the method name of the class is not a global symbol. The method names in a class do not conflict with the same method names in other classes. Two completely different classes can implement the same method.

The method name is part of the object interface. The name of the message received by the object is the name of the method called. Because different objects can have methods of the same name, objects must be able to understand the meaning of messages. The same message is sent to different objects, resulting in different operations.

The main benefit of polymorphism is that it simplifies programming interfaces. It allows you to reuse some habitual names between classes without naming a new name for each newly added function. In this way, the programming interface is a set of abstract behaviors, thus being differentiated from the classes that implement interfaces.

Objective-C supports square method name polymorphism, but does not support parameter and operator polymorphism.

 

Ii. How to Implement polymorphism in objective-C

 

In objective-C, it is implemented through a selector. In objective-C, selector has two meanings. When used to send source code messages to an object, it refers to the method name. It also refers to the unique identifier that replaces the method name after source code compilation. The type of the compiled selector is that sel has the same name and selector. You can use a selector to call the method of an object.

 

The selector has the following features:

* All methods with the same name have the same selector.

* All selectors are different.

 

(1) SEL and @ Selector

The selector type is Sel. @ Selector indicator is used to reference selector. The return type is Sel.

For example:

Sel responsesel;

Responsesel = @ selector (loaddatafortableview :);

The selector can be obtained through a string, for example:

Responsesel = nsselectorfromstring (@ "loaddatafortableview :");

You can also obtain the method name through reverse conversion, for example:

Nsstring * methodname = nsstringfromselector (responsesel );

 

(2) method and Selector

 

The selector determines the method name rather than the method implementation. This is the basis of polymorphism and dynamic binding. It makes it a reality to send the same message to different classes of objects. Otherwise, there is no difference between sending messages and calling methods in Standard C, it is impossible to support polymorphism and dynamic binding.

In addition, the same class method and instance method of the same class have the same selector.

(3) method return value and @ parameter type

 

The message mechanism finds the return value type and parameter type of the method through the selector. Therefore, it is dynamically bound (for example, sending a message to the object defined by the ID) the implementation of methods with the same name must have the same return value type and the same parameter type; otherwise, the corresponding method may not be found during running.

One exception is that although the same name stove method and instance method have the same selector, they can have different parameter types and return value types.

 

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.