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(@"长得帅呆了");
}