static let ambivalent = friendly + " but " + hostile
}
注意到上述代码中没有使用
self
。在静态
/
类代码中,
self
表示类型本身。即便在
self
会被隐式使用的场景下,我也倾向于显式使用它,不过这里却无法使用
self
,虽
然编译器不会报错(我认为这是个
Bug
)。为了表示
friendly
与
hostile
的状态
,我
可以使用类型名字,就像其他代码一样:
struct Greeting {
static let friendly = "hello there"
static let hostile = "go away"
static let ambivalent = Greeting.friendly + " but " + Greeting.hostile
}
另外,如果将 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.