sách gpt4 ăn đã đi

Swift:使用扩展和组合模式符合协议(protocol)

In lại 作者:搜寻专家 更新时间:2023-11-01 07:28:15 24 4
mua khóa gpt4 giày nike

在此 PDF 的幻灯片 62 上:Some Columbia College Presentation它说 Swift 及其扩展背后的想法之一是使用组合模式来符合协议(protocol)。

现在我明白了 Swift 中扩展的语法

//example from Apple:
extension SomeType: SomeProtocol, AnotherProtocol {
// implementations of not yet provided functions and properties of SomeType go here
}

但是组合模式与它有什么关系呢?这家伙的意思是 composite 描述的模式 đây , 正确的?有人可以提供一个如何使用它的最小示例吗?

câu trả lời hay nhất

Composition 是指“实现”接口(interface),在 Swift 中称为符合 protocols

栅栏的另一边是Inheritance,您可以在其中扩展类。

这里的主要问题是您只能扩展一个类,但可以根据需要符合任意多的接口(interface)(至少在 Swift 中)。因此,如果您的类想要成为 UITableViewDelegateUITableViewDataSource,您只能通过组合来实现。

接口(interface)/协议(protocol)为开发人员留下了更多的开放空间,而不是你可以使用哪些类。协议(protocol)仅sự định nghĩa函数和方法——并且在 Swift 属性中,它们本身具有一些类似函数的功能。另一方面,类可以包含变量、常量等。它们已经可以实现其中的一些——这是接口(interface)通常做不到的。

关于Swift:使用扩展和组合模式符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472643/

24 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress