Objective-C [protocol Reference problem], objectivecprotocol
-------------------------------------------
Protocol Reference
This part is a supplement to the protocol we have learned earlier. Therefore, we have little knowledge about protocol, but it is also very important. I hope you will be familiar with it:
Directly run the Code:
Main. m
# Import <Foundation/Foundation. h>
# Import "Student. h"
# Import "workProtocol. h"
//★Note that although the Student class complies with this Protocol, we still need. import the header file of this protocol in m (because we use the import protocol in the header file of the Student Class (@ protocol ......) we only let the system know that the Student class exists, but only through compilation)
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
Student * stu = [Student new];
[Stu run];
[Stu work];
}
Return 0;
}
WorkProtocol. h
# Import <Foundation/Foundation. h>
@ Protocol workProtocol <NSObject>
-(Void) work;
@ End
Student. h
# Import <Foundation/Foundation. h>
@ Protocol workProtocol; // We Don't Need To compile this protocol every time we compile it. We only need to compile it once during the runtime, so here we can just tell the compiler that workProtocol is a protocol. (The goal is to save compilation memory and time)
// We can also directly write a protocol in the. h file of a class, so that we can directly use this Protocol (work2)
@ Protocol work2 <NSObject>
-(Void) run;
@ End
@ Interface Student: NSObject <workProtocol, work2>
@ End
Student. m
# Import "Student. h"
# Import "workProtocol. h"
// We only need to use the import method when using the method in the Protocol in. m.
@ Implementation Student
-(Void) work
{
NSLog (@ "student work! ");
}
-(Void) run
{
NSLog (@ "run! ");
}
@ End
-------------------------------------------
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.