Protocol protocol Use

Source: Internet
Author: User

Protocol protocol:

The protocol is similar to the interface in C #

L used to declare the method

L if a class complies with an agreement, then it has all the statements of the agreement.

L can comply with multiple protocols

As long as the parent adheres to a protocol, the subclass adheres to that protocol.

Comply with the agreement <,>

Inheritance by:

1. Protocol Definition

@protocol protocol Name

@required//Require implementation, do not implement will issue a warning

-(void) test;

@optional//Do not need to implement

-(void) test2;

@end

2. How to comply with the agreement

1> Class Compliance Agreement

@interface Class Name: Parent class < protocol name,......>

Base agreement: Any agreement is subject to that agreement <NSObject>

L A protocol that complies with another agreement can have all the method declarations of this Agreement

2> Protocol Compliance Agreement

@protocol Child protocol name < Parent protocol,........>//Much like inheritance in a class but not inheritance

Limit the object type when you top a variable and follow a protocol

nsobject< protocol name > *obj = Class of Compliance protocol

Requires Obj5, the saved object must obey Myprotocol3, and inherit the person

Person<myprotocol3> *person;

Protocol protocol Use

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.