OBJECTIVE-C Protocol Protocol

Source: Internet
Author: User



 Everything is possible.


the protocol defines a set of methods for other classes to implement. The protocol itself is not a class. a protocol is a set of methods that are not implemented. A protocol is a group that has a certain correlation. the methods in the Protocol are implemented by other people
//
// main.m

#import <Foundation / Foundation.h>

#import "Student.h"

int main (int argc, const char * argv []) {
    @autoreleasepool {
        // insert code here ...
        NSLog (@ "Hello, World!");
        Student * stu = [[Student alloc] init];
        [stu finishTeask];
        [stu dontlate];
        [stu wearNeat]; // optional
   
    }
    return 0;
}
 

All default methods in the protocol must be implemented
The agreement has two keywords: @required and @optional
@required indicates the methods that must be implemented
@Optional means optional implementation
//
// Scsy.h

#import <Foundation / Foundation.h>
// protocol Scsy protocol <parent class>
@protocol Scsy <NSObject>
@ required // must be implemented
//statement
//finish homework
-(void) finishTeask;
//Do not be late
-(void) dontlate;
@ optional // optional
// Clean clothes
-(void) wearNeat;
//
// Student.h

#import <Foundation / Foundation.h>
#import "Scsy.h"
@interface Student: NSObject <Scsy>

@end
//
// Student.m

#import "Student.h"

@implementation Student
-(void) finishTeask {
    NSLog (@ "complete job");
}
-(void) dontlate {
    NSLog (@ "Cannot be late");
    
}
-(void) wearNeat {
    NSLog (@ "衣衣 tidy");
}
@end 



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.