@protocol 類似于java中的interface
1党瓮、聲明方法,不實(shí)現(xiàn)方法;
2龄广、定義屬性@property帅腌,僅是getter setter形式的聲明
@protocol 語(yǔ)法:
@protocol SportProtocol <NSObject>
- (void)playFootball;
- (void)playBasketball;
- (void)run;
@end
@protocol關(guān)鍵詞
@required:這個(gè)方法必須實(shí)現(xiàn)助隧,默認(rèn)就是required(若不實(shí)現(xiàn)痒给,編譯會(huì)報(bào)錯(cuò))
@optional:這個(gè)方法不一定要實(shí)現(xiàn)
示例:
@protocol SportProtocol <NSObject>
@required
- (void)playFootball;
- (void)playBasketball;
@optional
- (void)run;
- (void)readSportType;
@end
@interface類似于java的類聲明
1贪薪、使用協(xié)議的類查辩,必須實(shí)現(xiàn)協(xié)議中的方法(與java的實(shí)現(xiàn)類似)
2胖笛、一個(gè)類可以遵循1個(gè)或多個(gè)協(xié)議(與java的實(shí)現(xiàn)類似)
@interface語(yǔ)法:
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Person : NSObject<SportProtocol>
@end
@interface集成+協(xié)議
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Person : NSObject<SportProtocol>
@end
----------------------
#import "Person.h"
@interface Student : Person
@end
----------------------