# 02.5. 面向对象

## method

``````
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))
}``````

"A method is a function with an implicit first argument, called a receiver."

method 的语法如下：

``func (r ReceiverType) funcName(parameters) (results)``

``````
package main

import (
"fmt"
"math"
)

type Rectangle struct {
width, height float64
}

type Circle struct {
}

func (r Rectangle) area() float64 {
return r.width*r.height
}

func (c Circle) area() float64 {
}

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())
}
``````

• 虽然 method 的名字一模一样，但是如果接收者不一样，那么 method 就不一样
• method 里面可以访问接收者的字段
• 调用 method 通过 `.` 访问，就像 struct 里面访问字段一样

``````
type typeName typeLiteral``````

``````
type ages int

type money float32

type months map[string]int

m := months {
"January":31,
"February":28,
...
"December":31,
}``````

``````
package main

import "fmt"

const(
WHITE = iota
BLACK
BLUE
RED
YELLOW
)

type Color byte

type Box struct {
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
}

func (b *Box) SetColor(c Color) {
b.color = c
}

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 (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},
}

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("Obviously, now, the biggest one is", boxes.BiggestColor().String())
}``````

• Color 作为 byte 的别名
• 定义了一个 struct:Box，含有三个长宽高字段和一个颜色属性
• 定义了一个 slice:BoxList，含有 Box

• Volume() 定义了接收者为 Box，返回 Box 的容量
• SetColor(c Color)，把 Box 的颜色改为 c
• BiggestColor() 定在 BoxList 上面，返回 list 里面容量最大的颜色
• PaintItBlack() 把 BoxList 里面所有 Box 的颜色全部变成黑色
• String() 定义在 Color 上面，返回 Color 的具体颜色(字符串格式)

### method 继承

``````
package main

import "fmt"

type Human struct {
name string
age int
phone string
}

type Student struct {
Human // 匿名字段
school 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)
}

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()
}``````

### method 重写

``````
package main

import "fmt"

type Human struct {
name string
age int
phone string
}

type Student struct {
Human // 匿名字段
school 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)
}

// 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.
}

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()
}``````