Swift3 有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然?
有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然 例如,即使协议设置为类协议,也不允许使用类func或最终类func:Swift3 有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然?,swift3,protocols,Swift3,Protocols,有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然 例如,即使协议设置为类协议,也不允许使用类func或最终类func: protocol MyProtocol: class { final class func dummyClassMethod() } 或者在这种情况下,当符合以下条件时,允许类创建静态方法或类方法: protocol MyProtocol: class { static func dummyClassMethod() } 你不能这样做,因为
protocol MyProtocol: class {
final class func dummyClassMethod()
}
或者在这种情况下,当符合以下条件时,允许类创建静态方法或类方法:
protocol MyProtocol: class {
static func dummyClassMethod()
}
你不能这样做,因为苹果的文档明确表示,为了这个目的,只能使用
static
:
在协议中声明类
或静态
方法要求
声明,用静态
声明标记方法声明
修改器
资料来源:
当您在
类
中实现协议
的静态
方法时,在实现中使用类
或静态
没有区别
protocol ProtocolForClasses: class {
static func method()
}
class ClassOne: ProtocolForClasses {
class func method() {
}
}
class ClassTwo: ProtocolForClasses {
static func method() {
}
}
“在实现中使用
类
或静态
没有区别”→ 实际上有一个区别,静态
方法不能在子类中重写,而类
方法可以重写。