From 7841451d9623f72cc1d9d5c7975a583f86b2912e Mon Sep 17 00:00:00 2001 From: zhang3xing1 Date: Fri, 22 May 2015 04:53:20 +0800 Subject: [PATCH] Update 14_Initialization.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 示例的代码与官方原版有出入 --- source/chapter2/14_Initialization.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/chapter2/14_Initialization.md b/source/chapter2/14_Initialization.md index a656f213..7059c09b 100755 --- a/source/chapter2/14_Initialization.md +++ b/source/chapter2/14_Initialization.md @@ -118,12 +118,17 @@ let freezingPointOfWater = Celsius(fromKelvin: 273.15) ```swift struct Color { - let red = 0.0, green = 0.0, blue = 0.0 + let red, green, blue: Double init(red: Double, green: Double, blue: Double) { self.red = red self.green = green self.blue = blue } + init(white: Double) { + red = white + green = white + blue = white + } } ``` @@ -131,6 +136,7 @@ struct Color { ```swift let magenta = Color(red: 1.0, green: 0.0, blue: 1.0) +let halfGray = Color(white: 0.5) ``` 注意,如果不通过外部参数名字传值,你是没法调用这个构造器的。只要构造器定义了某个外部参数名,你就必须使用它,忽略它将导致编译错误: