我希望能够拥有具有静态属性(字段)的类,该属性可以是从基类继承或从协议“混合”得出。并且每个类都应该有自己对该属性的实现。这种需求可行吗?最好是不可变的。
class C1 {
static let stProperty = "my prorepty1"
}
class C2 {
static let stProperty = "my prorepty2"
}
你可以通过协议来实现这一点:
protocol SomeProtocol {
static var foo: String { get }
}
class One: SomeProtocol {
class var foo: String {
get {
return "This is One"
}
}
}
顺便说一句,我同意下面Rob Napier的看法,这个功能有点奇怪。我认为可能有用例,但我也认为可以使用其他语言功能更好地实现它们
这是可能的,但在Swift中要使其有用确实很困难。你打算如何引用该属性?我们从一个超级简单的实现开始:
protocol SomeProtocol {
static var prop: String { get }
}
class C1: SomeProtocol {
static let prop = "This is One"
}
func useProp(x: SomeProtocol) -> String {
return x.prop
// 'SomeProtocol' does not have a member named 'prop'
}
那不起作用。 x
是一个实例,但我需要类型。
// Accessing members of protocol type value 'SomeProtocol.Type' is unimplemented
func useProp(x: SomeProtocol.Type) -> String {
return x.prop
}
考虑到“未实现”这个词,这可能是未来的工作方式。但是今天并不适用。
func useProp(x: SomeProtocol) -> String {
// Accessing members of protocol type value 'SomeProtocol.Type' is unimplemented
return x.dynamicType.prop
}
同样的事情。
今天,你真的需要将它挂在对象本身上,而不是使用static
或class
:
protocol SomeProtocol {
var prop: String { get }
}
class C1: SomeProtocol {
let prop = "This is One"
}
func useProp(x: SomeProtocol) -> String {
return x.prop
}
我不会在这里说“混合”。如果你真的像Ruby中的“mixin”一样,那么Swift中就没有这样的东西。Swift人们经常将此功能称为“默认实现”,目前还不可能(尽管我确实希望它最终会出现)。在协议中,你唯一能做的就是说实现者必须以某种方式提供这个方法。你无法为他们提供它。或者从协议中“混合”
class
更改为static
,但在Xcode 6.2 / Swift 1.1中对我不起作用。那是Swift 1.2的变化吗? - Stefan Arentzprop
。 - IncertezaC1.prop
,您就强制 C1
实现 prop
。除非某个函数接受协议(而不是特定的类),否则通过协议获得什么好处?为什么不只是让类实现该属性,而无需要求使用协议? - Rob Napierclass
是引用类型,因此您不能在其中使用static
关键字(仅允许在struct
或enum
中使用),而class let prop =“This is One”
会出现错误:class variables not yet supported
。我建议改用计算属性。 - Bartłomiej Semańczykprotocol P {
class var stProperty: String { get }
}
class C1 {
class var stProperty: String {
return = "my property1"
}
}
class C2 {
class var stProperty: String {
return = "my property2"
}
}
使用方法:
C2.prop //"my property2"
如果你尝试:
C2.prop = "new value" //"cannot assign to the result of this expression"
static var stProperty: String { get }
。 - Julius