OC language category, extension, protocol

Source: Internet
Author: User

I. Categories

Adds a new method to a known class without breaking the encapsulation. Known classes can be classes of custom classes and systems.

1. Realization and Declaration of class purpose

Build a student class and add the category

(1). Declaration (in student+working. h )

The original class must be introduced, here is the Studnet class .

(2). Implementation (is in student+working. m in)

Note the introduction.

2. Use of class purposes

I. Add an instance variable to the class item

J. Multiple classes can be added to a class, but the class name and method name cannot be duplicated

K. Cannot override class intent method arbitrarily

3. Limitations of Class Purpose

I. Overriding the method of the parent class in the class may cause the super message to break because the method in the class takes precedence over the parent class.

J. Cannot add an instance variable in the class.

Two. Extension

The private method is declared by adding a class to the implementation file of its own class, extending the equivalent of an anonymous class item.

1. Disclaimer

In the studnet.m file, above the @implementation, the method is not required to implement the

The extension does not provide a name that must be implemented, and the supplied name may not be implemented.

2. Implement

In the studnet.m file, implement the method in @implementation and @end.

The results that were called and printed in MAIN.M are as follows:

Three. Agreement

Declare some methods to be implemented by other classes, or add methods to classes

1. Declaring an agreement

In the file

2. In the header file of the Import protocol

3. Adoption of agreements in documents

4. And implement the methods in the protocol in the file

OC language category, extension, protocol

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.