"objective-c" category extension agreement

Source: Internet
Author: User

1. Category

The class is to add a new method to the existing class. However, you cannot add an instance variable. such as the system class, we do not see his. m files, so there is no way to directly add methods to implement.

What are the usage scenarios for category:
1, the class contains a number of method implementations, and these methods require different team members to implement
2. When you are working with classes in the base Class library, you do not want to inherit these classes and only want to add some methods.

Category can achieve the above requirements, of course, there are also the use of category is to pay attention to the problem:

1. Category can access an instance variable of the original class, but cannot add an instance variable, if you want to add a variable, it is implemented by inheriting to create a subclass.

2, category can overload the method of the original class, it is not recommended to do so, this will overwrite the original class method. If you do want to overload it, it is implemented by inheriting the subclass.

3, and the common interface is different, in the category implementation of the file instance method as long as you do not call it you can not implement all the methods of the Declaration.

4. Category can add attributes, but attribute usage is too complex.

2. Extension

The role of extension is to define your own private methods.

The form and the same class, do not have to create a new file, just put the class purpose interface place of the file in the class you need to extend the. m file inside.

"Hello" will be printed when the outside [student Hello]. But the text method cannot be called in the outside world, [student text] is wrong. The private method text can only be invoked inside the. m file through self. The internal implementation is hidden.

3. Agreement

(1). The protocol declares a method that can be implemented by other classes. The agreement itself will not be fulfilled.

(2). The protocol is not a class, but an interface that other objects can implement.

Define a protocol:

#import <Foundation/Foundation.h>@protocol marryprotocol <NSObject>//  Define a marriage agreement, and the marriage agreement can also inherit other agreements. @required   // agreements that must be followed to implement the method inside the. m file of the class that complies with the Marryprotocol agreement. -(void) washclothes; -(void) cook; @optional  // Optional Protocol -(void) Makemoney; @end

Proxy mode: You have a need, but let others do it.

Define a men class

#import <Foundation/Foundation.h>#import"RoomService.h"@ Interface* name; @property (nonatomic,assign)ID<MarryProtocol> mate; // Define a proxy -(ID) Initwithname: (NSString *) name; -(void) needeat; @end

Define a Women class

#import <Foundation/Foundation.h>#import"marryprotocol"  <MarryProtocol>  // Compliance Agreement @property (nonatomic,retain) NSString * name; -(ID) Initwithname: (NSString *) name; @end

Inside the men's realization

-(void) needeat{    [self.mate  Cook];}

There is a method in the implementation of the women

-(void) cook{      NSLog (%@ "wonmen is cooking ");}

Inside the main function

Men.mate = women;[ Men Needeat];

Print out: Wonmen is cooking

4. The protocol can also be written in a class, when used to introduce the class header file.

"objective-c" category extension agreement

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.