相关疑难解决方法(0)

从常规方法调用协议默认实现

我想知道是否有可能实现这样的目标.
我有一个像这样的游乐场:

protocol Foo {
    func testPrint()
}

extension Foo {
    func testPrint() {
        print("Protocol extension call")
    }
}

struct Bar: Foo {
    func testPrint() {
        // Calling self or super go call default implementation
        self.testPrint()
        print("Call from struct")
    }
}


let sth = Bar()
sth.testPrint()
Run Code Online (Sandbox Code Playgroud)

我可以提供一个默认实现,extension但如果Bar需要默认实现中的所有内容以及其他内容,该怎么办?
它在某种程度上类似于调用es中的super.方法class来满足实现每个属性等的要求,但我认为没有可能实现相同的structs.

oop protocols swift swift2

75
推荐指数
3
解决办法
2万
查看次数

标签 统计

oop ×1

protocols ×1

swift ×1

swift2 ×1