【发布时间】:2016-01-16 10:18:59
【问题描述】:
我是 Swift 编程新手,在构建工厂设计模式时遇到了障碍。以下是我的代码:
protocol IInterface {
func InterfaceMethod() -> Void
}
public class SubClass: IInterface {
init() {}
func InterfaceMethod() { }
public func SubClassMethod() { }
}
public class Factory() {
class func createFactory() -> IInterface {
return SubClass()
}
}
最后,我尝试像下面这样访问它
let mgr = Factory.createFactory()
//calling Interface Method
mgr.InterfaceMethod() -- This works, when i keep a dot (mgr.) it shows the method name
//calling subclass method
mgr.SubClassMethod() -- This doesn't work, when i keep a dot (mgr.) it doesnt even show the subclass method name
即使我使用mgr.SubClassMethod,它也会抛出错误提示
IInterface 类型的值没有成员 SubClassMethod
我相信我只会暴露在协议方法中,尽管通过工厂返回子类对象
我浏览和看到的所有示例都只显示了如何使用协议指定的方法,但我还没有看到显示如何使用子类自己的方法而不是协议方法的示例
【问题讨论】:
标签: ios swift factory factory-pattern