Files
build-web-application-with-…/zh-tw/02.5.md
2019-02-26 01:40:54 +08:00

338 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 2.5 物件導向
前面兩章我們介紹了函式和struct那你是否想過函式當作struct的欄位一樣來處理呢今天我們就講解一下函式的另一種形態帶有接收者的函式我們稱為`method`
## method
現在假設有這麼一個場景你定義了一個struct叫做長方形你現在想要計算他的面積那麼按照我們一般的思路應該會用下面的方式來實現
```Go
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))
}
```
這段程式碼可以計算出來長方形的面積但是area()不是作為Rectangle的方法實現的類似物件導向裡面的方法而是將Rectangle的物件如r1,r2作為引數傳入函式計算面積的。
這樣實現當然沒有問題咯,但是當需要增加圓形、正方形、五邊形甚至其它多邊形的時候,你想計算他們的面積的時候怎麼辦啊?那就只能增加新的函式咯,但是函式名你就必須要跟著換了,變成`area_rectangle, area_circle, area_triangle...`
像下圖所表示的那樣, 橢圓代表函式, 而這些函式並不從屬於struct(或者以物件導向的術語來說並不屬於class)他們是單獨存在於struct外圍而非在概念上屬於某個struct的。
![](images/2.5.rect_func_without_receiver.png?raw=true)
圖2.8 方法和struct的關係圖
很顯然,這樣的實現並不優雅,並且從概念上來說"面積"是"形狀"的一個屬性,它是屬於這個特定的形狀的,就像長方形的長和寬一樣。
基於上面的原因所以就有了`method`的概念,`method`是附屬在一個給定的型別上的,他的語法和函式的宣告語法幾乎一樣,只是在`func`後面增加了一個receiver(也就是method所依從的主體)。
用上面提到的形狀的例子來說method `area()` 是依賴於某個形狀(比如說Rectangle)來發生作用的。Rectangle.area()的發出者是Rectangle area()是屬於Rectangle的方法而非一個外圍函式。
更具體地說Rectangle存在欄位 height 和 width, 同時存在方法area(), 這些欄位和方法都屬於Rectangle。
用Rob Pike的話來說就是
>"A method is a function with an implicit first argument, called a receiver."
method的語法如下
func (r ReceiverType) funcName(parameters) (results)
下面我們用最開始的例子用method來實現
```Go
package main
import (
"fmt"
"math"
)
type Rectangle struct {
width, height float64
}
type Circle struct {
radius float64
}
func (r Rectangle) area() float64 {
return r.width*r.height
}
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}
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裡面可以訪問接收者的欄位
- 呼叫method透過`.`訪問就像struct裡面訪問欄位一樣
圖示如下:
![](images/2.5.shapes_func_with_receiver_cp.png?raw=true)
圖2.9 不同struct的method不同
在上例method area() 分別屬於Rectangle和Circle 於是他們的 Receiver 就變成了Rectangle 和 Circle, 或者說這個area()方法 是由 Rectangle/Circle 發出的。
>值得說明的一點是圖示中method用虛線標出意思是此處方法的Receiver是以值傳遞而非參考傳遞是的Receiver還可以是指標, 兩者的差別在於, 指標作為Receiver會對例項物件的內容發生操作,而普通型別作為Receiver僅僅是以副本作為操作物件,並不對原例項物件發生操作。後文對此會有詳細論述。
那是不是method只能作用在struct上面呢當然不是咯他可以定義在任何你自訂的型別、內建型別、struct等各種型別上面。這裡你是不是有點迷糊了什麼叫自訂型別自訂型別不就是struct嘛不是這樣的哦struct只是自訂型別裡面一種比較特殊的型別而已還有其他自訂型別申明可以透過如下這樣的申明來實現。
```Go
type typeName typeLiteral
```
請看下面這個申明自訂型別的程式碼
```Go
type ages int
type money float32
type months map[string]int
m := months {
"January":31,
"February":28,
...
"December":31,
}
```
看到了嗎?簡單的很吧,這樣你就可以在自己的程式碼裡面定義有意義的型別了,實際上只是一個定義了一個別名,有點類似於c中的typedef例如上面ages替代了int
好了,讓我們回到`method`
你可以在任何的自訂型別中定義任意多的`method`,接下來讓我們看一個複雜一點的例子
```Go
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())
}
```
上面的程式碼透過const定義了一些常量然後定義了一些自訂型別
- Color作為byte的別名
- 定義了一個struct:Box含有三個長寬高欄位和一個顏色屬性
- 定義了一個slice:BoxList含有Box
然後以上面的自訂型別為接收者定義了一些method
- Volume()定義了接收者為Box返回Box的容量
- SetColor(c Color)把Box的顏色改為c
- BiggestColor()定在在BoxList上面返回list裡面容量最大的顏色
- PaintItBlack()把BoxList裡面所有Box的顏色全部變成黑色
- String()定義在Color上面返回Color的具體顏色(字串格式)
上面的程式碼透過文字描述出來之後是不是很簡單?我們一般解決問題都是透過問題的描述,去寫相應的程式碼實現。
### 指標作為receiver
現在讓我們回過頭來看看SetColor這個method它的receiver是一個指向Box的指標是的你可以使用*Box。想想為啥要使用指標而不是Box本身呢
我們定義SetColor的真正目的是想改變這個Box的顏色如果不傳Box的指標那麼SetColor接受的其實是Box的一個copy也就是說method內對於顏色值的修改其實只作用於Box的copy而不是真正的Box。所以我們需要傳入指標。
這裡可以把receiver當作method的第一個引數來看然後結合前面函式講解的傳值和傳參考就不難理解
這裡你也許會問了那SetColor函式裡面應該這樣定義`*b.Color=c`,而不是`b.Color=c`,因為我們需要讀取到指標相應的值。
你是對的其實Go裡面這兩種方式都是正確的當你用指標去訪問相應的欄位時(雖然指標沒有任何的欄位)Go知道你要透過指標去取得這個值看到了吧Go的設計是不是越來越吸引你了。
也許細心的讀者會問這樣的問題PaintItBlack裡面呼叫SetColor的時候是不是應該寫成`(&bl[i]).SetColor(BLACK)`因為SetColor的receiver是*Box而不是Box。
你又說對了這兩種方式都可以因為Go知道receiver是指標他自動幫你轉了。
也就是說:
>如果一個method的receiver是*T,你可以在一個T型別的例項變數V上面呼叫這個method而不需要&V去呼叫這個method
類似的
>如果一個method的receiver是T你可以在一個*T型別的變數P上面呼叫這個method而不需要 *P去呼叫這個method
所以你不用擔心你是呼叫的指標的method還是不是指標的methodGo知道你要做的一切這對於有多年C/C++程式設計經驗的同學來說,真是解決了一個很大的痛苦。
### method繼承
前面一章我們學習了欄位的繼承那麼你也會發現Go的一個神奇之處method也是可以繼承的。如果匿名欄位實現了一個method那麼包含這個匿名欄位的struct也能呼叫該method。讓我們來看下面這個例子
```Go
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重寫
上面的例子中如果Employee想要實現自己的SayHi,怎麼辦簡單和匿名欄位衝突一樣的道理我們可以在Employee上面定義一個method重寫了匿名欄位的方法。請看下面的例子
```Go
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()
}
```
上面的程式碼設計的是如此的美妙讓人不自覺的為Go的設計驚歎
透過這些內容我們可以設計出基本的物件導向的程式了但是Go裡面的物件導向是如此的簡單沒有任何的私有、公有關鍵字透過大小寫來實現(大寫開頭的為公有,小寫開頭的為私有),方法也同樣適用這個原則。
## links
* [目錄](<preface.md>)
* 上一章: [struct型別](<02.4.md>)
* 下一節: [interface](<02.6.md>)