From 594fd4fdeb25d6ec699b00844d069336dfbaadd0 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 10 Jun 2017 11:40:56 +0800 Subject: [PATCH] Remove 02.5.md spaces --- zh/02.5.md | 330 ++++++++++++++++++++++++++--------------------------- 1 file changed, 165 insertions(+), 165 deletions(-) diff --git a/zh/02.5.md b/zh/02.5.md index 671fc278..3038f330 100644 --- a/zh/02.5.md +++ b/zh/02.5.md @@ -5,24 +5,24 @@ 现在假设有这么一个场景,你定义了一个struct叫做长方形,你现在想要计算他的面积,那么按照我们一般的思路应该会用下面的方式来实现 ```Go - package main +package main - import "fmt" +import "fmt" - type Rectangle struct { - width, height float64 - } +type Rectangle struct { + width, height float64 +} - func area(r Rectangle) float64 { - return r.width*r.height - } +func area(r Rectangle) float64 { + return r.width*r.height +} - func main() { - r1 := Rectangle{12, 2} - r2 := Rectangle{9, 4} - fmt.Println("Area of r1 is: ", area(r1)) - fmt.Println("Area of r2 is: ", area(r2)) - } +func main() { + r1 := Rectangle{12, 2} + r2 := Rectangle{9, 4} + fmt.Println("Area of r1 is: ", area(r1)) + fmt.Println("Area of r2 is: ", area(r2)) +} ``` 这段代码可以计算出来长方形的面积,但是area()不是作为Rectangle的方法实现的(类似面向对象里面的方法),而是将Rectangle的对象(如r1,r2)作为参数传入函数计算面积的。 @@ -53,41 +53,41 @@ method的语法如下: 下面我们用最开始的例子用method来实现: ```Go - package main +package main - import ( - "fmt" - "math" - ) +import ( + "fmt" + "math" +) - type Rectangle struct { - width, height float64 - } +type Rectangle struct { + width, height float64 +} - type Circle struct { - radius float64 - } +type Circle struct { + radius float64 +} - func (r Rectangle) area() float64 { - return r.width*r.height - } +func (r Rectangle) area() float64 { + return r.width*r.height +} - func (c Circle) area() float64 { - return c.radius * c.radius * math.Pi - } +func (c Circle) area() float64 { + return c.radius * c.radius * math.Pi +} - func main() { - r1 := Rectangle{12, 2} - r2 := Rectangle{9, 4} - c1 := Circle{10} - c2 := Circle{25} +func main() { + r1 := Rectangle{12, 2} + r2 := Rectangle{9, 4} + c1 := Circle{10} + c2 := Circle{25} - fmt.Println("Area of r1 is: ", r1.area()) - fmt.Println("Area of r2 is: ", r2.area()) - fmt.Println("Area of c1 is: ", c1.area()) - fmt.Println("Area of c2 is: ", c2.area()) - } + fmt.Println("Area of r1 is: ", r1.area()) + fmt.Println("Area of r2 is: ", r2.area()) + fmt.Println("Area of c1 is: ", c1.area()) + fmt.Println("Area of c2 is: ", c2.area()) +} ``` @@ -110,23 +110,23 @@ method的语法如下: 那是不是method只能作用在struct上面呢?当然不是咯,他可以定义在任何你自定义的类型、内置类型、struct等各种类型上面。这里你是不是有点迷糊了,什么叫自定义类型,自定义类型不就是struct嘛,不是这样的哦,struct只是自定义类型里面一种比较特殊的类型而已,还有其他自定义类型申明,可以通过如下这样的申明来实现。 ```Go - type typeName typeLiteral +type typeName typeLiteral ``` 请看下面这个申明自定义类型的代码 ```Go - type ages int +type ages int - type money float32 +type money float32 - type months map[string]int +type months map[string]int - m := months { - "January":31, - "February":28, - ... - "December":31, - } +m := months { + "January":31, + "February":28, + ... + "December":31, +} ``` 看到了吗?简单的很吧,这样你就可以在自己的代码里面定义有意义的类型了,实际上只是一个定义了一个别名,有点类似于c中的typedef,例如上面ages替代了int @@ -135,79 +135,79 @@ method的语法如下: 你可以在任何的自定义类型中定义任意多的`method`,接下来让我们看一个复杂一点的例子 ```Go - package main - - import "fmt" +package main - const( - WHITE = iota - BLACK - BLUE - RED - YELLOW - ) +import "fmt" - type Color byte +const( + WHITE = iota + BLACK + BLUE + RED + YELLOW +) - type Box struct { - width, height, depth float64 - color Color - } +type Color byte - type BoxList []Box //a slice of boxes +type Box struct { + width, height, depth float64 + color Color +} - func (b Box) Volume() float64 { - return b.width * b.height * b.depth - } +type BoxList []Box //a slice of boxes - func (b *Box) SetColor(c Color) { - b.color = c - } +func (b Box) Volume() float64 { + return b.width * b.height * b.depth +} - func (bl BoxList) BiggestColor() Color { - v := 0.00 - k := Color(WHITE) - for _, b := range bl { - if bv := b.Volume(); bv > v { - v = bv - k = b.color - } - } - return k - } +func (b *Box) SetColor(c Color) { + b.color = c +} - func (bl BoxList) PaintItBlack() { - for i, _ := range bl { - bl[i].SetColor(BLACK) +func (bl BoxList) BiggestColor() Color { + v := 0.00 + k := Color(WHITE) + for _, b := range bl { + if bv := b.Volume(); bv > v { + v = bv + k = b.color } } + return k +} - func (c Color) String() string { - strings := []string {"WHITE", "BLACK", "BLUE", "RED", "YELLOW"} - return strings[c] +func (bl BoxList) PaintItBlack() { + for i, _ := range bl { + bl[i].SetColor(BLACK) + } +} + +func (c Color) String() string { + strings := []string {"WHITE", "BLACK", "BLUE", "RED", "YELLOW"} + return strings[c] +} + +func main() { + boxes := BoxList { + Box{4, 4, 4, RED}, + Box{10, 10, 1, YELLOW}, + Box{1, 1, 20, BLACK}, + Box{10, 10, 1, BLUE}, + Box{10, 30, 1, WHITE}, + Box{20, 20, 20, YELLOW}, } - func main() { - boxes := BoxList { - Box{4, 4, 4, RED}, - Box{10, 10, 1, YELLOW}, - Box{1, 1, 20, BLACK}, - Box{10, 10, 1, BLUE}, - Box{10, 30, 1, WHITE}, - Box{20, 20, 20, YELLOW}, - } + fmt.Printf("We have %d boxes in our set\n", len(boxes)) + fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm³") + fmt.Println("The color of the last one is",boxes[len(boxes)-1].color.String()) + fmt.Println("The biggest one is", boxes.BiggestColor().String()) - fmt.Printf("We have %d boxes in our set\n", len(boxes)) - fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm³") - fmt.Println("The color of the last one is",boxes[len(boxes)-1].color.String()) - fmt.Println("The biggest one is", boxes.BiggestColor().String()) + fmt.Println("Let's paint them all black") + boxes.PaintItBlack() + fmt.Println("The color of the second one is", boxes[1].color.String()) - fmt.Println("Let's paint them all black") - boxes.PaintItBlack() - fmt.Println("The color of the second one is", boxes[1].color.String()) - - fmt.Println("Obviously, now, the biggest one is", boxes.BiggestColor().String()) - } + fmt.Println("Obviously, now, the biggest one is", boxes.BiggestColor().String()) +} ``` 上面的代码通过const定义了一些常量,然后定义了一些自定义类型 @@ -252,81 +252,81 @@ method的语法如下: 前面一章我们学习了字段的继承,那么你也会发现Go的一个神奇之处,method也是可以继承的。如果匿名字段实现了一个method,那么包含这个匿名字段的struct也能调用该method。让我们来看下面这个例子 ```Go - package main +package main - import "fmt" +import "fmt" - type Human struct { - name string - age int - phone string - } +type Human struct { + name string + age int + phone string +} - type Student struct { - Human //匿名字段 - school string - } +type Student struct { + Human //匿名字段 + school string +} - type Employee struct { - Human //匿名字段 - company string - } +type Employee struct { + Human //匿名字段 + company string +} - //在human上面定义了一个method - func (h *Human) SayHi() { - fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) - } +//在human上面定义了一个method +func (h *Human) SayHi() { + fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) +} - func main() { - mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} - sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} +func main() { + mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} + sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} - mark.SayHi() - sam.SayHi() - } + mark.SayHi() + sam.SayHi() +} ``` ### method重写 上面的例子中,如果Employee想要实现自己的SayHi,怎么办?简单,和匿名字段冲突一样的道理,我们可以在Employee上面定义一个method,重写了匿名字段的方法。请看下面的例子 ```Go - package main - - import "fmt" +package main - type Human struct { - name string - age int - phone string - } +import "fmt" - type Student struct { - Human //匿名字段 - school string - } +type Human struct { + name string + age int + phone string +} - type Employee struct { - Human //匿名字段 - company string - } +type Student struct { + Human //匿名字段 + school string +} - //Human定义method - func (h *Human) SayHi() { - fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) - } +type Employee struct { + Human //匿名字段 + company string +} - //Employee的method重写Human的method - func (e *Employee) SayHi() { - fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, - e.company, e.phone) //Yes you can split into 2 lines here. - } +//Human定义method +func (h *Human) SayHi() { + fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) +} - func main() { - mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} - sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} +//Employee的method重写Human的method +func (e *Employee) SayHi() { + fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, + e.company, e.phone) //Yes you can split into 2 lines here. +} - mark.SayHi() - sam.SayHi() - } +func main() { + mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} + sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} + + mark.SayHi() + sam.SayHi() +} ``` 上面的代码设计的是如此的美妙,让人不自觉的为Go的设计惊叹!