发布于 2015-06-14 01:55:41 | 160 次阅读 | 评论: 0 | 来源: 网络整理

集合中的协议类型

协议类型可以被集合使用,表示集合中的元素均为协议类型:


let things: TextRepresentable[] = [game,d12,simoTheHamster]

如下所示,things数组可以被直接遍历,并调用其中元素的asText()函数:


for thing in things {
    println(thing.asText())
}
// A game of Snakes and Ladders with 25 squares
// A 12-sided dice
// A hamster named Simon

thing被当做是TextRepresentable类型而不是DiceDiceGameHamster等类型。因此能且仅能调用asText方法

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务