第07天OC语言(01):协议基本概念
2017-07-22 本文已影响14人
liyuhong
- 不要等到明天,明天太遥远,今天就行动。
须读:看完该文章你能做什么?
1.知道什么是协议,如何使用协议
学习前:你必须会什么?(在这里我已经默认你具备C语言的基础了)
适合所有人,不需要懂的什么
一、本章笔记
/*
protocol
@interface 类名 : 父类<协议名称1,协议名称2>
@end
*/
二、code
main.m
#pragma mark 01-协议基本概念
#pragma mark 概念
#pragma mark - 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#import "Person.h"
#import "Student.h"
#import "Teacher.h"
#pragma mark - main函数
int main(int argc, const char * argv[])
{
Person *p = [[Person alloc]init];
[p playFootball];
Student *s = [[Student alloc]init];
[s playBasketball];
Teacher *t = [[Teacher alloc]init];
[t playBaseball];
return 0;
}
Person
>>>.h
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Person : NSObject<SportProtocol>
//- (void)playFootball;
@end
>>>.m
#import "Person.h"
@implementation Person
- (void)playFootball
{
NSLog(@"%s",__func__);
}
- (void)playBasketball
{
NSLog(@"%s",__func__);
}
- (void)playBaseball
{
NSLog(@"%s",__func__);
}
@end
Student
>>>.h
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Student : NSObject<SportProtocol>
@end
>>>.m
#import "Student.h"
@implementation Student
- (void)playFootball
{
NSLog(@"%s",__func__);
}
- (void)playBasketball
{
NSLog(@"%s",__func__);
}
- (void)playBaseball
{
NSLog(@"%s",__func__);
}
@end
Teacher
>>>.h
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Teacher : NSObject<SportProtocol>
@end
>>>.m
#import "Teacher.h"
@implementation Teacher
- (void)playFootball
{
NSLog(@"%s",__func__);
}
- (void)playBasketball
{
NSLog(@"%s",__func__);
}
- (void)playBaseball
{
NSLog(@"%s",__func__);
}
@end
Dog
>>>.h
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Dog : NSObject<SportProtocol>
@end
>>>.m
#import "Dog.h"
@implementation Dog
- (void)playFootball
{
NSLog(@"%s",__func__);
}
- (void)playBasketball
{
NSLog(@"%s",__func__);
}
- (void)playBaseball
{
NSLog(@"%s",__func__);
}
@end