OBJECTIVE-C Protocol (Protocol)

Source: Internet
Author: User

The role of the Protocol is similar to the multiple inheritance of abstract base classes in C + +. Similar to the concept of an Interface (interface) in Java.
A protocol is a list of methods that are shared by multiple classes, and the methods listed in the Protocol are not implemented in this class, but are other classes that implement these methods.

If a class is to adhere to a protocol, the class must implement all the methods of a particular protocol (except for optional methods).

Defining a protocol requires the use of the @protocol directive, followed by the protocol name, and then you can declare some methods, and the declarations of all methods before the instruction @end are part of the protocol. As follows:

[CPP]View Plaincopy
    1. @protocol nscopying
    2. -(ID) Copywithzone: (nszone*) zone;
    3. @end


If your class decides to abide by the Nscopying protocol, you must implement the Copywithzone method. By listing the name of the protocol in a pair of angle brackets in @interface, tell the compiler that you are following a protocol such as:
@interface Test:nsobject <NSCopying>

Instance:
Fly.h

[CPP]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. @protocol Fly
    3. -(void) go;
    4. -(void) stop;
    5. @optional
    6. -(void) sleep;
    7. @end


FlyTest.h

[CPP]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. #import "Fly.h"
    3. @interface flytest:nsobject<fly> {
    4. }
    5. @end


Flytest.m

[CPP]View Plaincopy
    1. #import "FlyTest.h"
    2. @implementation Flytest
    3. -(void) go {
    4. NSLog (@"Go");
    5. }
    6. -(void) Stop {
    7. NSLog (@"Stop");
    8. }
    9. @end


Test.m

[CPP]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. #import "FlyTest.h"
  3. int main ( int argc, char* argv[]) {
  4. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
  5. Flytest *flytest = [[Flytest alloc]init];
  6. [Flytest go];
  7. [Flytest stop];
  8. [Flytest release];
  9. [Pool drain];
  10. return 0;
  11. }


The results of the program run as follows:

Go
Stop


The standard syntax for @protocol is:
@protocol Agreement name < other agreements, ...>
Method declaration 1
@optional
Method Declaration 2
@required
Method Declaration 3
...
@end


@optional indicates that the class of the Protocol does not necessarily implement the method.
@required is a method that must be implemented.



OBJECTIVE-C Protocol (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.