//
Main.m
05-protoco
//
Created by Apple on 13-8-11.
Copyright (c) 2013 itcast. All rights reserved.
//
/*
1. Definition of the Agreement
@protocol Agreement name <NSObject>
Method declaration List ....
@end
2. How to comply with the agreement
1> Class Compliance Agreement
@interface Class Name: Parent class name < protocol name 1, protocol name 2>
@end
2> Protocol Compliance Agreement
@protocol Agreement name < Other protocol name 1, other protocol name 2>
@end
3. Keywords for method declarations in the agreement
1> @required (default)
Requires implementation, and if not implemented, a warning is issued
2> @optional
Does not require implementation, how can there be no warning
4. When defining a variable, limit the object that this variable holds to a protocol
Class name < protocol name > * variable name;
id< protocol name > variable name;
Nsobject<myprotocol> *obj;
Id<myprotocol> Obj2;
If the corresponding protocol is not followed, the compiler warns
The attributes declared in [email protected] can also be used as a limit to comply with the agreement
@property (Nonatomic, Strong) class name < protocol name > * attribute name;
@property (nonatomic, strong) id< protocol name > attribute name;
@property (nonatomic, strong) dog<myprotocol> *dog;
@property (nonatomic, strong) id<myprotocol> dog2;
6. The agreement can be defined in a separate. h file and can also be defined in a class
1> If this protocol is used only in a class, the protocol should be defined in that class
2> If this protocol is used in many classes, it should be defined in a separate file
7. Classifications can be defined in separate. h and. m files and can also be defined in the original class
1> in general, are defined in a separate file
2> defines the classification in the original class, only requires the ability to read the grammar
*/
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
#import "MyProtocol3.h"
#import "Person.h"
#import "Dog.h"
#import "Hashiqi.h"
int main ()
{
Person *p = [[Person alloc] init];
P.obj = [[Hashiqi alloc] init];
return 0;
}
void Test ()
{
NSObject *obj = [[NSObject alloc] init];
NSObject *obj2 = @ "4324324";
The object required for OBJ3 to be preserved must be myprotocol this protocol
nsobject<myprotocol> *obj3 = [[NSObject alloc] init];
nsobject<myprotocol> *obj3 = [[Person alloc] init];
Obj3 = nil;
id<myprotocol> OBJ4 = [[Person alloc] init];
Obj4 = nil;
Requires Obj5, the saved object must obey MyProtocol3, and inherit the person
person<myprotocol3> *obj5 = [[Person alloc] init];
Obj5 = nil;
}
15-02-28-protocol [email protected] interface