Extension of the class

Source: Internet
Author: User

In OC we often have to extend the functionality for a class, with the following ideas:

Subclass: Create A. Class directly inherits from the original class and expands the new functionality in the class, which can either extend the method or augment the instance variable. However, you want to use an expanded function, must make?? The object of the subclass, the object of the original class has no expansion function.

Modify Source code: This is the most direct way to add functionality, but limited to the need to have the source code of the class , we have the right to modify. The method can either be expanded or the instance variable can be expanded.

Protocol: This is the way of indirect expansion of the protocol, only a bunch of methods in the declaration, so that when the class is required to obey the protocol, implement the method in the protocol to expand the function, and can only expand the method, can not expand the instance variable . limited to having the source code for that class .

Category: Class, for non-source code class extension functions, such as the system provided by the Nsstring,nsarray class, we can not get their source code, but we want to add functionality for these classes, such as printing, compared to the count, we could use the category , to augment the method, but not to extend the instance variable .

EXtension: extension, very simple, for we know the source code of the class, add private instance variables and methods, specifically declared in the. m file. Other classes cannot invoke instance variables and methods that access the private, and can only be used in. m files of that class.

Extension of the class

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.