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