AssociatedType与Self.AssociatedType之间的区别
创始人
2024-09-20 19:01:09
0

将AssociatedType定义为类型的协议,使用关联类型(associatedtype)指定此协议的类型实现,使用Self.AssociatedType指定协议实现者的类型。

AssociatedType定义:

protocol SomeProtocol {
   associatedtype ItemType
   func append(_ item: ItemType)
}

struct Stack: SomeProtocol {
   // 用 T 替代 ItemType
   typealias ItemType = T
   var items = [ItemType]()
   mutating func push(_ item: ItemType) {
       items.append(item)
   }
   mutating func pop() -> ItemType {
       return items.removeLast()
   }
   // 遵循协议 实现使用另一种类型的方法
   mutating func append(_ item: Int) {
       self.push(item as! T)
   }
}

Self.AssociatedType定义:

protocol SomeProtocol {
   associatedtype ItemType
   func append(_ item: Self.ItemType)
}

struct Stack: SomeProtocol {
   var items = [T]()
   mutating func push(_ item: T) {
       items.append(item)
   }
   mutating func pop() -> T {
       return items.removeLast()
   }
   // 遵循协议 实现使用另一种类型的方法
   mutating func append(_ item: Int) {
       self.push(item as! T)
   }
}

相关内容

热门资讯

现有说明如下"hhp... 现有说明如下"hhpoker是真的假的"wepoker底牌透视脚本(竟然有辅助神器)-哔哩哔哩1、全...
透视代打"wepok... 透视代打"wepoker一直输的号能继续打吗"竞技联盟辅助插件(竟然真的是有辅助下载)-哔哩哔哩1)...
今天下午"hhpok... 今天下午"hhpoker德州透视"werplan外卦神器(其实真的是有辅助工具)-哔哩哔哩1、hhp...
透视脚本"哈糖大菠萝... 透视脚本"哈糖大菠萝攻略"wepoker辅助器怎么弄(真是有辅助工具)-哔哩哔哩1、很好的工具软件,...
据权威媒体报道"aa... 据权威媒体报道"aapoker辅助插件工具"菠萝德州透视脚本(竟然是有辅助软件)-哔哩哔哩1、金币登...
相较于以往"约局吧开... 相较于以往"约局吧开挂"hh poker辅助有用吗(切实存在有辅助神器)-哔哩哔哩所有人都在同一条线...
目前来看"wepok... 目前来看"wepoker有透视底牌吗"如何下载德普之星辅助软件(切实有辅助工具)-哔哩哔哩1、如何下...
透视模拟器"哈糖大菠... 透视模拟器"哈糖大菠萝能开挂吗"wepoker透视脚本视频(确实真的是有辅助辅助器)-哔哩哔哩1、进...
网友热议"aapok... 网友热议"aapoker万能辅助器"pokemmo脚本辅助(原来真的有辅助下载)-哔哩哔哩1、每一步...
随着"德扑HHpok... 随着"德扑HHpoker有挂吗"wepoker怎么破解游戏(竟然真的有辅助工具)-哔哩哔哩1、每一步...