第07天OC语言(01):协议基本概念

  • 不要等到明天,明天太遥远,今天就行动。
须读:看完该文章你能做什么?

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不要等到明天,明天太遥远,今天就行动。 须读:看完该文章你能做什么? 协议的一些注意点,required关键字,o...
    liyuhong阅读 1,482评论 0 0
  • 不要等到明天,明天太遥远,今天就行动。 须读:看完该文章你能做什么? 代理设计模式的基本概念代理设计模式的使用场合...
    liyuhong阅读 1,521评论 0 0
  • 2014年的苹果全球开发者大会(WWDC),当Craig Federighi向全世界宣布“We have new ...
    yeshenlong520阅读 6,794评论 0 9
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,899评论 18 399
  • 又是一年春好处,绝胜烟柳满皇都。 不知不觉,后知后觉,又是几度春秋。 苍茫间,在岁月悠悠里,度过了无数个大好时光。...
    小孜阅读 1,215评论 0 0