zoukankan      html  css  js  c++  java
  • Object—C 块在函数中作为参数时的分析

    暂时对这个有了一些粗浅的理解,记下来一边后面学习时学习,改正。

    先举个例子:

    A类:

    .h文件:

    @interface A  : NSObject

    - (void)Paly1:(void (^)(double salary))game1

               Paly2:(void (^)(double salary))game2;

     

    A类:

    .m文件:

    - (void)Paly1:(void (^)(double salary))game1

               Paly2:(void (^)(double salary))game2{

               double zsalary;

               scanf("%lf",&zsalary);

               if(zsalary>5000){

                Play1(zsalary);

              }else{

               Play2(zsalary);

              }

    }

    @end;

    B 类:

    .h文件:

    @interface B :NSObject 

    - (void)Play;

    @end;

     

    .m 文件:

     

    @implementation B

    - (void)Play{

               A *a = [A new];

               [a Play1:^(double salary)game1{

               NSLog(" 超过5000了,玩game1去!");

               } Play2:^(doule salary)game2{

               NSLog(" 靠,还没有5000,只能玩game2了!");
               }];

    }

    @end;

     

    通过Block可以实现回调,也常说成委托。当程序运行时,虽然B类的函数调用类A类的含Block的函数,但B类并没有真正的权利去调用这个Block,只能调用A,真正由权利去调用Blocks的是A类定义的函数,这是通过这个函数在A类中定义的语句来判断具体进入哪个Block中。通过这个就是回调,委托。

    具体的详细解说参见(牛人博客):http://www.cnblogs.com/ygm900/p/3633131.html

     

  • 相关阅读:
    计算几何中的精度问题
    codeforces上分计划
    洛谷P1962 斐波那契数列 (矩阵快速幂)
    codeforces 1474 E
    codeforces 1474 C
    codeforces 1467 E
    牛客练习赛76 F phi and phi (莫比乌斯反演)
    牛客练习赛76 D 魔物消灭计划 (斯坦纳树)
    牛客练习赛76 E 牛牛数数 (线性基)
    codeforces 1467 D. Sum of Paths (dp)
  • 原文地址:https://www.cnblogs.com/chaiwentao/p/3993135.html
Copyright © 2011-2022 走看看