The polymorphism of OC object-oriented characteristics

Source: Internet
Author: User

Objective-c language is an object-oriented high-level programming language, so it has some characteristics of object-oriented programming, namely: encapsulation, inheritance and polymorphism.

Today, we introduce the polymorphism of Objective-c.

First, what is polymorphic

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, this makes polymorphism known as possible. Names in the class definition do not conflict with the name outside the class definition. class instance variables and class methods have the following characteristics:

    • Like a data member in a struct in C, an instance variable of a class is also located in a namespace that is unique to that class.

    • The class method is also located in the namespace that is unique to the class. Unlike the method name in C, the method name of a class is not a global symbol. The method names in one 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 being called. Because different objects can have methods with the same name, the object must be able to understand the meaning of the message. The same message is sent to different objects, and the actions that result are not the same.

The main benefit of polymorphism is that it simplifies programming interfaces. It allows for the reuse of some habitual naming between classes and classes without naming a new name for each new function. In this way, the programming interface is a collection of abstract behavior that distinguishes it from the class that implements the interface.

OBJECTIVE-C supports the polymorphism of method names, but does not support the polymorphism of parameters and operators.

Ii. how to realize polymorphism in Objective-c

The objective-c is implemented by a selector called selector. In Objective-c, selector has two meanings, used to refer to the name of the method when used in the source message to the object. It also refers to the unique identifier that replaces the method name after the source code is compiled. The type of the compiled selector is the same way that the SEL has the same name, and the same selector. You can use selectors to invoke methods of an object.

The selector has the following characteristics:

* All methods with the same name have the same picker

* All the pickers are not the same

(1) Sel and @selector

The type of selector is SEL. The @selector indicator is used to refer to the selector, and the return type is sel.

For example:

SEL Responsesel;

Responsesel = @selector (loaddatafortableview:);

You can get the picker through a string, for example:

Responsesel = nsselectorfromstring (@ "Loaddatafortableview:");

You can also get the method name by reverse conversion, for example:

NSString *methodname = Nsstringfromselector (Responsesel);

(2) method and Selector

The picker determines the method name, not the method implementation. This is the basis for polymorphism and dynamic binding, which makes it possible to send the same message to non-homogeneous objects, otherwise there is no difference between sending a message and calling a method in standard C, and it is impossible to support polymorphism and dynamic binding.

In addition, class methods of the same name and instance methods of the same class have the same picker.

(3) method return value and @ parameter type

The message mechanism finds the return value type and parameter type of the method through the picker, so dynamic binding (example: sending a message to an object defined by an ID) requires that the implementation of a method of the same name have the same return value type and the same parameter type; otherwise, the runtime may not find the corresponding method error.

There is one exception, although the same name and instance methods have the same picker, but they can have different parameter types and return value types

The polymorphism of OC object-oriented characteristics

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.