小白學開發(iOS)OC_ protocol(協議) (2015-08-09)

//

//  main.m

//  protocol(協議)

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

/*

  1. 什麼是協議:

    >  協議實際上是一種向上抽象的思想

    >  協議只是做一些共有方法的聲明,不做實現,哪個類使用,則哪個類自己實現

 

  2. 協議的使用方式:

    >  建立協議:添加protocol文件,一般命名上要加Protoclo字段

    >  使用協議:需要使用的協議的類在.h文件中先import引入,再在類名後添加 <協議名>,再在.M文件中實現方法

 

  3. 注意:

    >  一個類中可以使用多個協議,<協議1,協議2,。。。>

    >  協議本身也可以遵循其他協議,NSObject是一個基礎協議,協議也可以同時遵循多個其他協議

    >  在類的繼承中,父類遵循的協議,子類同樣遵循(即:當通過子類對象調用協議中方法時,先在子類實現中找,找不大則會在父類實現中找)

    >  一個類特有的協議我們可以把它直接在這個類的.h文件中直接聲明出來寫在@interface之前,.m文件中實現

    >  協議中的方法有必須實現(@required)的和可選擇實現(@optional)的,分別用括號中的關鍵字區分

    >  協議中只能定義方法,不能定義變量

 

  4. 聲明一個需要遵循某個協議的變量

    > 局部變量: Person < 協議名 > *p = [[Person alloc]init];

    > 成員變量: @property(nonatomic,strong) Car<協議名> *car

  */



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

    @autoreleasepool {

        Person *p = [[Person alloc]init];

        [p reading];

        [p song];

        Student *s = [[Student alloc]init];

        [s reading];    //  子類調用類父類中實現的協議的方法

        [s song];

        

        [s taoke];  //  調用學生類中特有協議的方法

        // 定義一個需要遵循StudentProtocol的變量

        Person<StudentProtocol> *p1 = [[Person alloc]init];

    }

    return 0;

}

//

//  Person.h

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "StudentProtocol.h" // 引入協議文件

#import "TeacherProtocol.h"


@interface Person : NSObject <StudentProtocol,TeacherProtocol>

 // 定義一個需要遵循StudentProtocol的變量

@property (nonatomic,strong)NSString<StudentProtocol> *name;

// 定義一個需要遵循TeacherProtocol的變量

@property (nonatomic,assign)id<TeacherProtocol> obj;


@end


//

//  Person.m

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import "Person.h"

//  實現協議中的方法

@implementation Person


- (void)listen

{

    NSLog(@"學生要聽課");

}

- (void)reading

{

    NSLog(@"學生要讀書");

}

-(void)play

{

    NSLog(@"學生要玩");

}

- (void)teach

{

    NSLog(@"老師要傳道授業解惑");

}

-(void)care

{

    NSLog(@"老師要關心學生");

}

-(void)song     // 這是TeacherProtocol協議中一個選擇實現的方法

{

    NSLog(@"老師最好會唱歌");

}

@end


//

//  StudentProtocol.h

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol StudentProtocol <NSObject>


@required

- (void)listen;

- (void)reading;

- (void)play;


@optional

- (void)dajia;


@end


//

//  TeacherProtocol.h

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol TeacherProtocol <NSObject>


@required

- (void)teach;

- (void)care;

@optional

- (void)song;


@end


//

//  TeacherProtocol.h

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol TeacherProtocol <NSObject>


@required

- (void)teach;

- (void)care;

@optional

- (void)song;


@end


//

//  Student.h

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import "Person.h"

@protocol StuProtocol<NSObject>


- (void)taoke;


@end


@interface Student : Person <StuProtocol>


@end


//

//  Student.m

//  OC的特有語法

//

//  Created by admin on 15/8/12.

//  Copyright (c) 2015 admin. All rights reserved.

//


#import "Student.h"


@implementation Student


- (void)taoke

{

    NSLog(@"學生逃課,這個是學上類特有的協議");

}


@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章