第07天OC语言(05):代理设计模式练习及规范

  • 不要等到明天,明天太遥远,今天就行动。

#####须读:看完该文章你能做什么?

熟悉代理的使用

#####学习前:你必须会什么?(在这里我已经默认你具备C语言的基础了)

如何声明一个协议,协议的注意点


#####一、本章笔记

/*
 协议的编写规范:
 1.一般情况下,当前协议属于谁,我们就将协议定义到谁的头文件中
 
 2.协议的名称 一般以它 属于的那个类的类名开头,后面跟上protocol或者delegate
 StudentProtocol / StudentDelegate
 3.协议中的方法名称 一般以协议的名称protocol之前的作为开头
 studentFindHourse
 4.一般情况下 协议中的方法 会将 触发该协议的对象传递出去
 - (void)studentFindHourse:(Student *)student;
 5.一般情况下 一个类的代理属性的名称 叫 delegate;
 6.当某一个类 要成为另外一个类的代理的时候,
 一般情况下在.h中 用  @protocol 协议名称; 告诉当前类,这是一个协议
 在.m中用 #import 真正的导入一个协议的声明
 @protocol StudentProtocol;
 */

#####二、code ######main.m

#pragma mark 05-代理设计模式练习及规范
#pragma mark - 代码
#import <Foundation/Foundation.h>
#pragma mark 类

#import "Student.h"
#import "LinkHome.h"
#import "LoveHome.h"
#pragma mark - main函数
int main(int argc, const char * argv[])
{
    /*
     用代理实现学生找房子,不具备找房子的能力
     所以学生可以招另一个对象 来帮它找房子,那么另一个对象就是学生的代理
     */
    
    
    Student *stu = [Student new];
    LinkHome *lj = [LinkHome new];
    stu.delegate = lj;
    
//    LoveHome *lh = [LoveHome new];
//    stu.delegate = lh;

    [stu findHourse];
    
    return 0;
}

######Student

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

/*
 协议的编写规范:
 1.一般情况下,当前协议属于谁,我们就将协议定义到谁的头文件中
 
 2.协议的名称 一般以它 属于的那个类的类名开头,后面跟上protocol或者delegate
    StudentProtocol / StudentDelegate
 3.协议中的方法名称 一般以协议的名称protocol之前的作为开头
    studentFindHourse
 4.一般情况下 协议中的方法 会将 触发该协议的对象传递出去
    - (void)studentFindHourse:(Student *)student;
 5.一般情况下 一个类的代理属性的名称 叫 delegate;
 6.当某一个类 要成为另外一个类的代理的时候,
    一般情况下在.h中 用  @protocol 协议名称; 告诉当前类,这是一个协议
    在.m中用 #import 真正的导入一个协议的声明
    @protocol StudentProtocol;
 */
@class Student;
@protocol StudentProtocol <NSObject>

// 帮学生找房子
- (void)studentFindHourse:(Student *)student;

@end
@interface Student : NSObject

@property (nonatomic,strong) id<StudentProtocol> delegate;

- (void)findHourse;

@end

>>>.m
#import "Student.h"

@implementation Student
- (void)findHourse
{
    NSLog(@"学生想找房子");
    // 通知代理帮他找房子
    if ([self.delegate respondsToSelector:@selector(studentFindHourse:)]) {
        [self.delegate studentFindHourse:self];
    }
}
@end

######LinkHome

>>>.h
#import <Foundation/Foundation.h>
//#import "Student.h"
// 声明
@protocol StudentProtocol;
@interface LinkHome : NSObject<StudentProtocol>

@end

>>>.m
#import "LinkHome.h"
#import "Student.h"

@implementation LinkHome

- (void)studentFindHourse:(Student *)student
{
    NSLog(@"%s",__func__);
}


@end

######LoveHome

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

@protocol StudentProtocol;
@interface LoveHome : NSObject<StudentProtocol>

@end

>>>.m
#import "LoveHome.h"
#import "Student.h"

@implementation LoveHome

- (void)studentFindHourse:(Student *)student
{
     NSLog(@"%s",__func__);
}

@end

######StudentProtocol

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

@protocol StudentProtocol <NSObject>

// 帮学生找房子
- (void)studentFindHourse;

@end

转载于:https://my.oschina.net/u/3561184/blog/1475109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值