美文网首页
iOS 协议

iOS 协议

作者: 印林泉 | 来源:发表于2017-03-02 17:42 被阅读108次
  • 协议
    只做方法声明。本质是一组方法列表,用来规范接口。

  • 应用,适用场景
    用对象本身体现客观实体,用类对对象进行描述,通过让类遵守相应协议,根据具体业务逻辑进行对应实现来规范对象对行为。协议的遵守者一定是类本身。按照协议规定的方法执行。

创建,制定协议

//
//  PersonProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol PersonProtocol <NSObject>

@required
- (void)eat;

@optional
- (void)work;
- (void)play;

@end
//
//  PoliceProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol PoliceProtocol <NSObject>

- (void)catchThief;

@end
//
//  ThiefProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol ThiefProtocol <NSObject>

- (void)steal;

@end

遵守

//
//  Person.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PersonProtocol.h"

@interface Person : NSObject<PersonProtocol>

@end
//
//  Person.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"

@implementation Person

- (void)eat {
    NSLog(@"eat");
}

@end

//
//  Police.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"
#import "PoliceProtocol.h"

@interface Police : Person<PoliceProtocol>

@end
//
//  Police.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Police.h"

@implementation Police

- (void)eat {
    NSLog(@"eat");
}

- (void)work {
    NSLog(@"抓小偷");
}

- (void)catchThief {
    [self work];
}

@end

//
//  Thief.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"
#import "ThiefProtocol.h"

@interface Thief : Person<ThiefProtocol>

@end
//
//  Thief.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Thief.h"

@implementation Thief

- (void)steal {
    NSLog(@"steal");
}

@end

使用

//
//  ViewController.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
#import "Police.h"
#import "Thief.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    Person *person = [[Person alloc] init];
    [person eat];
    //[person work];//未实现,调用崩溃
    
    Police *police = [[Police alloc] init];
    [police catchThief];
    
    Thief *thief = [[Thief alloc] init];
    [thief steal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

<NSObject>是一个协议,不是类。是父协议。

相关文章

网友评论

      本文标题:iOS 协议

      本文链接:https://www.haomeiwen.com/subject/jdgcgttx.html