Objective-c Text Summary

Source: Internet
Author: User

Polymorphism literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.

Polymorphism refers to the same entity having multiple forms at the same time. It is an important feature of object-oriented programming (OOP). If a language supports only classes and does not support polymorphism, it can only indicate that it is object-based, not object-oriented.

Polymorphism: The same operation acts on different objects, can have different interpretations, and produces different execution results. At run time, you can invoke methods in the derived class by pointing to a pointer to the base class.

Polymorphism in OC: different objects respond differently to the same message. Subclasses can override methods of the parent class

Polymorphism is the ability to allow a method to duplicate a parameter or return value that can be passed in or returned by a parent type.

Consider the different subclass objects as the parent class, you can block the differences between different sub-class objects, write generic code, and make general programming to adapt to the changing needs.

After the assignment, the parent object can operate differently depending on the attributes of the child object that is currently assigned to it. In other words, the father behaves like a son, not a son acting like a father.

For example: Deriving from a base class responds to a virtual command, producing different results.

The result of using inheritance is that you can create a family of classes that, when you know the family of this class, treat the object of the exported class as the object of the base class. The importance of this understanding is that we can write a program only for the base class, but it can be adapted to the family of the class, because the compiler will automatically find the appropriate object to perform the operation. This phenomenon is also known as polymorphism. And the means of realizing polymorphism is called dynamic binding.

Simply put, create a parent class object, its content can be this parent class, it can be its subclass, when the subclass has the same function as the parent class, when using this object call this function, the definition of the object's class (that is, the parent class) in the name of the function will be called, When the Mute keyword is added to the function in the parent class, the function with the same name as the child class is called. Popular point is that the parent class does not add the mute keyword, then the child class of the same name function will be overwritten.

Summary

Method
1, the parent class object type points to the subclass object.

Advantages
1, in the parameters of a function or method, the parent class object type can accept multiple-seed-class-object-passed parameters at the same time.
2, when a subclass object is pointed to by the parent object type, you can call the parent class method directly, but you cannot call the specific method of the subclass directly, and if necessary, you need to cast the object type to subclass type.

Limitations
1, the Subclass object type cannot point to the parent class object. (the compiler warns you, but it works, or it is not recommended)
2, a subclass that inherits from the same parent class object cannot point to each other. (the compiler warns you, but it works, or it is not recommended)


Objective-c Text Summary

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.