Learn Java students know Interface (interface), then there is no interface in the objective-c? In fact objective-c with protocol (protocol) to achieve, in objective-c specific how to use, we look directly at the code example.
Studentprotocol
///////////////////// .h ///////////////////////
#import <Foundation / Foundation.h>
@protocol StudentProtocol <NSObject>
@optional // The following method is optional
-(void) fallInLove: (NSString *) name;
@required // The following methods must be implemented
-(void) curriculum;
@end
Studentprotocol has written how to use it, we take the student class as an example, student to achieve studentprotocol only in the Student.h class name after adding <studentprotocol> STUDENT.M implement the methods defined in the Studentprotocol.
/////////////////.h //////////////////////
#import "Person.h"
#import "StudentProtocol.h"
@interface Student: Person <StudentProtocol>
-(id) initWithName: (NSString *) name sex: (NSString *) sex age: (int) age;
@end
///////////////// .m /////////////////////
#import "Student.h"
@implementation Student
-(id) initWithName: (NSString *) name sex: (NSString *) sex age: (int) age
{
self = [super init];
if (self) {
self.name = name;
self.sex = sex;
self.age = age;
}
return self;
}
-(Person *) work
{
NSLog (@ "% @ is working", self.name);
return 0;
}
-(void) printInfo {
NSLog (@ "My name is:% @ This year is% d years old. I am a% @% @", self.name, self.age, self.sex, NSStringFromClass ([self class]));
}
#pragma mark StudentProtocol
-(void) fallInLove: (NSString *) name {
NSLog (@ "I'm still a student, I can talk about love or not ... but I'm still in love with --->% @ <---" Love, "name);
}
-(void) curriculum {
NSLog (@ "I am a student and must study in class ...");
}
@end
In Studentprotocol declaration of two methods, there is an optional implementation, then we have no way to know the Student to achieve this method? Yes, keep looking at the code.
#import "AppDelegate.h"
#import "Student.h"
#import "StudentProtocol.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
Student * s = [[Student alloc] initWithName: @ "xiaoming" sex: @ "male" age: 12];
if ([s conformsToProtocol: @protocol (StudentProtocol)]) {// determine if a protocol is followed
NSLog (@ "I followed --- StudentProtocol --- protocol");
if ([s respondsToSelector: @selector (fallInLove :)]) {// Judge if there is a method
[s fallInLove: @ "ruhua"];
}
}
return YES;
}
@end
Test results:
2015-06-14 18:23:13.104 attendance[16464:617950] I followed ---studentprotocol--- protocol .
2015-06-14 18:23:13.104 attendance[16464:617950] I am still a student , talk About love can ..., but I still ---> like flowers <--- fall in love.
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4575504.html
[Objective-c] 006_protocol (protocol)