Protocol 保护,私有,公有

Main.m

#import "Student.h"
#import "Teacher.h"

int main(int argc, const char * argv[])
{

    Student *student = [[Student alloc] init];
    //调用本身的方法
    [student work];
    
    //调用协议方法
    //必须实现的协议方法
    [student dontLate];
    [student dontSleep];
    [student finishWork];
    //可以选择
//    [student wearNeat];   不严谨
    
    //判断是否实现了wearNeat,如果实现了则调用
    if ([student respondsToSelector:@selector(wearNeat)]) {
        [student wearNeat];
    }
    
    Teacher *teacher = [[Teacher alloc] init];
    [teacher work];
    
    [teacher teachClass];
    
    return 0;
}

Students.h

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

@protocol WXHLProtocol <NSObject>

@required   //用这个词修饰的协议方法必须实现
//不迟到
- (void)dontLate;

//按时完成作业
- (void)finishWork;

//不可睡觉
- (void)dontSleep;

@optional   //可选择的协议方法

//穿着整洁
- (void)wearNeat;

@end

@interface Student : NSObject<WXHLProtocol> //签署协议

//本身的方法
- (void)work;

Students.m

@implementation Student
//自己的方法
- (void)work {

    NSLog(@"学生学习了");
    
}

#pragma mark - WXHLProtocol
//不迟到
- (void)dontLate {

    NSLog(@"不迟到");
    
}

//按时完成作业
- (void)finishWork {

    NSLog(@"完成作业");
    
}

#warning 过几天加代码
//不可睡觉
- (void)dontSleep {

    NSLog(@"上课不睡觉");
    
}

//穿着整洁
- (void)wearNeat {

    NSLog(@"穿着整洁");
    
}

Teacher.h

@required   //必须实现的方法
- (void)teachClass; //讲课
- (void)solveQuestion;  //解决问题

@optional

- (void)handSome;

@end

@interface Teacher : NSObject<TeacherProtocol>

//本身的方法

- (void)work;

Teacher.m

//本身的方法

- (void)work {

    NSLog(@"讲课");
    
}

//必须实现的方法
- (void)teachClass {

    NSLog(@"给学生讲课");
    
}

- (void)solveQuestion {

    NSLog(@"解决学习问题");
    
}

- (void)handSome {

    NSLog(@"长得帅呆了");
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值