关于协议的迅速扩展

时间:2017-01-20 00:48:35

标签: swift3 protocols extension-methods

当我注释时,如何理解波纹管代码' func makeMargherita()' 比萨店协议,结果是

["tomato", "mozzarel"]
["tomato", "basil", "mozzarel"]

但是当func makeMargherita()工作时,它会打印出来:

["tomato", "basil", "mozzarel"]
["tomato", "basil", "mozzarel"]

如何理解这段代码?

protocol Pizzeria{
  func makePizza(ingredients: [String])
    //func makeMargherita()
}

extension Pizzeria{
func makeMargherita() {
    return makePizza(ingredients: ["tomato", "mozzarel"])
 }
}

struct Lombardis: Pizzeria{

func makePizza(ingredients: [String]) {
    print(ingredients)
  }
func makeMargherita() {
    return makePizza(ingredients: ["tomato", "basil", "mozzarel"])
  }
}

let lombardis1: Pizzeria = Lombardis()
let lombardis2: Lombardis = Lombardis()
lombardis1.makeMargherita()
lombardis2.makeMargherita()

0 个答案:

没有答案