From 85f747ce4b4b5542e21ff3d4dd3a5451f62c3084 Mon Sep 17 00:00:00 2001 From: Shiny Zhu Date: Mon, 9 Jun 2014 16:22:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E2=80=9C=E7=B1=BB?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E7=9A=84=E8=AF=AD=E6=B3=95=E2=80=9D=E4=B8=80?= =?UTF-8?q?=E8=8A=82=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/10_Properties.md | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/source/chapter2/10_Properties.md b/source/chapter2/10_Properties.md index c5d7fe79..5a3bde50 100644 --- a/source/chapter2/10_Properties.md +++ b/source/chapter2/10_Properties.md @@ -298,5 +298,36 @@ stepCounter.totalSteps = 896 > > 跟实例的存储属性不同,必须给存储型类属性指定默认值,因为类型本身无法在初始化过程中使用构造器给类属性赋值。 -###类属性的语法 +###类属性语法 + +在 C 或 Objective-C 中,静态常量和静态变量的定义是通过特定类型加上`global`关键字。在 Swift 编程语言中,类属性是作为类型定义的一部分写在类型最外层的花括号内,因此它的作用范围也就在类型支持的范围内。 + +使用关键字`static`来定义值类型的类属性,关键字`class`来为类(class)定义类属性。下面的例子演示了存储型和计算型类属性的语法: + +``` +struct SomeStructure { + static var storedTypeProperty = "Some value." + static var computedTypeProperty: Int { + // return an Int value here + } +} +enum SomeEnumeration { + static var storedTypeProperty = "Some value." + static var computedTypeProperty: Int { + // return an Int value here + } +} +class SomeClass { + class var computedTypeProperty: Int { + // return an Int value here + } +} + +``` + +> 注意 +> +> 例子中的计算型类属性是只读的,但也可以定义可读可写的计算型类属性,跟实例计算属性的语法类似。 + +###获取和设置类属性的值