Swift 链式变成思想
作者:
追逐_chase | 来源:发表于
2017-11-28 16:51 被阅读0次

Swift.png
//创建一个计算类
class CaculateMaker {
//存储属性
var result:Int = 0;
//返回自身
func add(n:Int) -> CaculateMaker {
result += n;
return self;
}
func sub(n:Int) -> CaculateMaker {
result -= n;
return self;
}
}
class Caculate {
//类型函数,闭包参数么有 返回值 类似BLock
static func beginCaculate(caculateBlock:(CaculateMaker)->()) ->Int {
let caculator = CaculateMaker();
caculateBlock(caculator);
return caculator.result;
}
}
//调用函数
let result = Caculate.beginCaculate { (maker) in
maker.add(n: 2).sub(n: 1).add(n: 5);
}
print(result);
本文标题:Swift 链式变成思想
本文链接:https://www.haomeiwen.com/subject/sajebxtx.html
网友评论