[关闭]
@fiy-fish 2015-07-19T18:17:07.000000Z 字数 2201 阅读 1324

day09-02-协议

Objective-c


  1. // main.m
  2. // day09-02-协议
  3. //
  4. // Created by Aaron on 15/7/13.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. //协议
  9. //网络协议
  10. //应用协议
  11. //OC有协议这种语法
  12. //专门用来规定方法
  13. #import "Student.h"
  14. #import "JiangShi.h"
  15. #import "BanZhuRen.h"
  16. #import "Dog.h"
  17. int main(int argc, const char * argv[]) {
  18. @autoreleasepool {
  19. JiangShi *j = [[JiangShi alloc] init];
  20. Student *std = [[Student alloc] init];
  21. //[std askToTeacher:j];
  22. BanZhuRen *b = [[BanZhuRen alloc] init];
  23. //[std askToTeacher:b];
  24. [std askToSomeOne:j];
  25. Dog *d = [[Dog alloc] init];
  26. [std askToSomeOne:d];
  27. }
  28. return 0;
  29. }

  1. #import "Teacher.h"
  2. @interface BanZhuRen : Teacher
  3. //-(NSString *)banZhuRenAnswer;
  4. @end

  1. #import "BanZhuRen.h"
  2. @implementation BanZhuRen
  3. //-(NSString *)banZhuRenAnswer
  4. //{
  5. // return @"考得非常好";
  6. //}
  7. @end

  1. #import <Foundation/Foundation.h>
  2. //#import "Teacher.h"
  3. //#import "JiangShi.h"
  4. //#import "BanZhuRen.h"
  5. //协议的制定
  6. @protocol StudentScore <NSObject>
  7. //协议里面规定的是方法
  8. -(NSInteger)answerScore;
  9. @end
  10. @interface Student : NSObject
  11. //-(void)askToJiangShi:(JiangShi *)j;
  12. //-(void)askToBanZhuRen:(BanZhuRen *)b;
  13. //-(void)askToTeacher:(Teacher *)teacher;
  14. //让someOne遵守协议
  15. -(void)askToSomeOne:(id <StudentScore>) someOne;
  16. @end

  1. #import "Student.h"
  2. @implementation Student
  3. //-(void)askToJiangShi:(JiangShi *)j
  4. //{
  5. // NSLog(@"讲师,我考了多少分?");
  6. // NSLog(@"讲师回答:%ld",
  7. // [j jiangShiAnswer]);
  8. //}
  9. //
  10. //-(void)askToBanZhuRen:(BanZhuRen *)b
  11. //{
  12. // NSLog(@"讲师,我考了多少分?");
  13. // NSLog(@"班主任回答:%@",
  14. // [b banZhuRenAnswer]);
  15. //}
  16. //-(void)askToTeacher:(Teacher *)teacher
  17. //{
  18. // NSLog(@"考了多少分");
  19. // NSLog(@"考了%ld分",
  20. // [teacher teacherAnswer]);
  21. //}
  22. -(void)askToSomeOne:(id <StudentScore>) someOne
  23. {
  24. NSLog(@"考了多少分");
  25. //协议的作用是统一接口
  26. //统一接口用协议更常用,因为代码更通用,耦合性更低
  27. NSLog(@"考了%ld分",
  28. [someOne answerScore]);
  29. }
  30. @end

  1. import "Teacher.h"
  2. #import "Student.h"
  3. //包含头文件
  4. //遵循协议,<协议名>
  5. @interface JiangShi : Teacher <StudentScore>
  6. //-(NSInteger)jiangShiAnswer;
  7. @end

  1. #import "JiangShi.h"
  2. @implementation JiangShi
  3. //-(NSInteger)jiangShiAnswer
  4. //{
  5. // return 98;
  6. //}
  7. //遵循了协议,就要实现协议的方法
  8. //协议里面的方法,不需要再声明
  9. -(NSInteger)answerScore
  10. {
  11. return 98;
  12. }
  13. @end

  1. #import <Foundation/Foundation.h>
  2. #import "Student.h"
  3. @interface Dog : NSObject <StudentScore>
  4. @end

  1. import "Dog.h"
  2. @implementation Dog
  3. -(NSInteger)answerScore
  4. {
  5. return 10000;
  6. }
  7. @end

  1. #import <Foundation/Foundation.h>
  2. @interface Teacher : NSObject
  3. -(NSInteger)teacherAnswer;
  4. @end

  1. #import "Teacher.h"
  2. @implementation Teacher
  3. -(NSInteger)teacherAnswer
  4. {
  5. return 99;
  6. }
  7. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注