Fixed something wrong.

This commit is contained in:
Oling Cat
2012-09-22 16:00:26 +08:00
parent a39d2cf8fe
commit 65aa4e36d2
4 changed files with 276 additions and 276 deletions

272
2.5.md
View File

@@ -6,23 +6,23 @@
package main
import "fmt"
type Rectangle struct {
width, height float64
}
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))
width, height float64
}
这个代码是可以计算出来长方形的面积但是area不是作为Rectangle的一个方法(类似面向对象里面的方法)实现的, 而是Rectangle的对象(r1,r2)作为函数的一个参数传入,然后计算获取的。
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))
}
这个代码是可以计算出来长方形的面积但是area不是作为Rectangle的一个方法类似面向对象里面的方法实现的而是Rectangle的对象r1,r2作为函数的一个参数传入然后计算获取的。
这样实现有什么问题,当然没有问题咯,但是当你代码里面增加一个圆形、正方形、多边形等的时候,然后你又想计算他们的面积的时候怎么办啊?那就增加新的函数咯,但是函数名你就必须要跟着换了,你就要把函数名变成`area_rectangle, area_circle, area_triangle...`
@@ -34,9 +34,9 @@
>“A method is a function with an implicit first argument, called a receiver.“
method的语法如下
func (ReceiverType r) func_name (parameters) (results)
method的语法如下
func (ReceiverType r) funcName(parameters) (results)
下面我们用最开始的例子用method来实现
@@ -45,36 +45,36 @@ method的语法如下
"fmt"
"math"
)
type Rectangle struct {
width, height float64
width, height float64
}
type Circle struct {
radius float64
radius float64
}
func (r Rectangle) area() float64 {
return r.width*r.height
return r.width*r.height
}
func (c Circle) area() float64 {
return c.radius * c.radius * math.Pi
return c.radius * c.radius * math.Pi
}
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())
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())
}
在使用method的时候重要注意几点
- 虽然method的名字一模一样但是如果接收者不一样那么method就不一样
@@ -83,8 +83,8 @@ method的语法如下
那是不是method只能作用在struct上面呢当然不是咯他可以定义在任何你自定义的类型、内置类型、struct等各种类型上面。这里你是不是有点迷糊了什么叫自定义类型自定义类型不就是struct嘛不是这样的哦struct只是自定义类型里面一种比较特殊的类型而已还有其他自定义类型申明可以通过如下这样的申明来实现。
type type_name type_literal
type typeName typeLiteral
请看下面这个申明自定义类型的代码
type ages int
@@ -94,12 +94,12 @@ method的语法如下
type months map[string]int
m := months {
"January":31,
"February":28,
...
"December":31,
}
"January":31,
"February":28,
...
"December":31,
}
看到了吗?简单的很吧,这样你就可以在自己的代码里面定义有意义的类型了,实际上只是一个定义了一个别名,有点类似于c中的typedef例如上面ages替代了int
好了,让我们回到`method`
@@ -110,75 +110,75 @@ method的语法如下
import "fmt"
const(
WHITE = iota
BLACK
BLUE
RED
YELLOW
WHITE = iota
BLACK
BLUE
RED
YELLOW
)
type Color byte
type Box struct {
width, height, depth float64
color Color
width, height, depth float64
color Color
}
type BoxList []Box //a slice of boxes
func (b Box) Volume() float64 {
return b.width * b.height * b.depth
return b.width * b.height * b.depth
}
func (b *Box) SetColor(c Color) {
b.color = c
b.color = c
}
func (bl BoxList) BiggestsColor() Color {
v := 0.00
k := Color(WHITE)
for _, b := range bl {
if b.Volume() > v {
v = b.Volume()
k = b.color
}
}
return k
v := 0.00
k := Color(WHITE)
for _, b := range bl {
if b.Volume() > v {
v = b.Volume()
k = b.color
}
}
return k
}
func (bl BoxList) PaintItBlack() {
for i, _ := range bl {
bl[i].SetColor(BLACK)
}
for i, _ := range bl {
bl[i].SetColor(BLACK)
}
}
func (c Color) String() string {
strings := []string {"WHITE", "BLACK", "BLUE", "RED", "YELLOW"}
return strings[c]
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},
}
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.BiggestsColor().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.BiggestsColor().String())
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.BiggestsColor().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.BiggestsColor().String())
}
上面这个例子我们通过const定义了一些常量然后定义了一些自定义类型
- Color作为byte的别名
@@ -223,34 +223,34 @@ method的语法如下
package main
import "fmt"
type Human struct {
name string
age int
phone string
name string
age int
phone string
}
type Student struct {
Human //匿名字段
school string
Human //匿名字段
school string
}
type Employee struct {
Human //匿名字段
company string
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)
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"}
mark.SayHi()
sam.SayHi()
mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi()
sam.SayHi()
}
###method重载
@@ -258,49 +258,49 @@ method的语法如下
package main
import "fmt"
type Human struct {
name string
age int
phone string
name string
age int
phone string
}
type Student struct {
Human //匿名字段
school string
Human //匿名字段
school string
}
type Employee struct {
Human //匿名字段
company string
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)
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
//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.
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.
}
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 := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi()
sam.SayHi()
}
上面的代码设计的是如此的美妙让人不自觉的为Go的设计惊叹
通过这些内容我们可以设计出基本的面向对象的程序了但是Go里面的面向对象是如此的简单没有任何的私有、共有关键字通过大小写来实现(大写开头的为共有,小写开头的为私有),方法也同样适用这个原则。
通过这些内容我们可以设计出基本的面向对象的程序了但是Go里面的面向对象是如此的简单没有任何的私有、共有关键字通过大小写来实现(大写开头的为共有,小写开头的为私有),方法也同样适用这个原则。
## links
* [目录](<preface.md>)
* 上一章: [struct类型](<2.4.md>)
* 下一节: [interface](<2.6.md>)
## LastModified
## LastModified
* $Id$