Objective-C [protocol Reference problem], objectivecprotocol

Source: Internet
Author: User

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.

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.