Go學習 10 方法

2021-08-31 13:33:27 字數 4495 閱讀 1113

go 語言中同時有函式和方法。乙個方法就是乙個包含了接受者的函式,接受者可以是命名型別或者結構體型別的乙個值或者是乙個指標。所有給定型別的方法屬於該型別的方法集

方法只是乙個函式,它帶有乙個特殊的接收器型別,它是在func關鍵字和方法名之間編寫的。接收器可以是struct型別或非struct型別。接收方可以在方法內部訪問。

定義方法的語法

func

(t type)

methodname

(parameter list)

例項**:

package main

import

("fmt"

)type employee struct

/* displaysalary() method has employee as the receiver type

*/func

(e employee)

displaysalary()

func

main()

emp1.

displaysalary()

//calling displaysalary() method of employee type

}

可以定義相同的方法名

示例**:

package main

import

("fmt"

"math"

)type rectangle struct

type circle struct

func

(r rectangle)

area()

float64

//該 method 屬於 circle 型別物件中的方法

func

(c circle)

area()

float64

func

main()

r2 := rectangle

c1 := circle

c2 := circle

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

)}

執行結果

area of r1 is:  24

area of r2 is: 36

area of c1 is: 314.1592653589793

area of c2 is: 1963.4954084936207

既然我們已經有了函式,為什麼還要使用方法?

示例**:

package main

import

("fmt"

)type employee struct

/* displaysalary() method converted to function with employee as parameter

*/func

displaysalary

(e employee)

func

main()

displaysalary

(emp1)

}

在上面的程式中,displaysalary方法被轉換為乙個函式,而employee struct作為引數傳遞給它。這個程式也產生了相同的輸出:salary of sam adolf is $5000.。

為什麼我們可以用函式來寫相同的程式呢?有以下幾個原因

go不是一種純粹物件導向的程式語言,它不支援類。因此,型別的方法是一種實現類似於類的行為的方法。

相同名稱的方法可以在不同的型別上定義,而具有相同名稱的函式是不允許的。假設我們有乙個正方形和圓形的結構。可以在正方形和圓形上定義乙個名為area的方法。這是在下面的程式中完成的。

作用域為已宣告識別符號所表示的常量、型別、變數、函式或包在源**中的作用範圍。

go 語言中變數可以在三個地方宣告:

區域性變數

在函式體內宣告的變數稱之為區域性變數,它們的作用域只在函式體內,引數和返回值變數也是區域性變數。

全域性變數

在函式體外宣告的變數稱之為全域性變數,首字母大寫全域性變數可以在整個包甚至外部包(被匯出後)使用。

package main

import

"fmt"

/* 宣告全域性變數 */

var g int

func

main()

結果

結果: a =

10, b =

20 and g =

30

形式引數

形式引數會作為函式的區域性變數來使用

指標作為接收者

若不是以指標作為接收者,實際只是獲取了乙個copy,而不能真正改變接收者的中的資料

func

(b *box)

setcolor

(c color)

示例**

package main

import

("fmt"

)type rectangle struct

func

(r *rectangle)

setval()

func

main()

s := p

p.setval()

fmt.

println

(p.height, s.height)

}

結果

20

2

如果沒有那個*,則值就是2 2

method是可以繼承的,如果匿名字段實現了乙個method,那麼包含這個匿名欄位的struct也能呼叫該method

package main

import

"fmt"

type human struct

type student struct

type employee struct

func

(h *human)

sayhi()

func

main()

,"mit"

} sam := employee

,"golang inc"

} mark.

sayhi()

sam.

sayhi()

}

執行結果:

hi, i am mark you can call me on 222

-222

-yyyy

hi, i am sam you can call me on 111

-888

-***x

package main

import

"fmt"

type human struct

type student struct

type employee struct

//human定義method

func

(h *human)

sayhi()

//employee的method重寫human的method

func

(e *employee)

sayhi()

func

main()

,"mit"

} sam := employee

,"golang inc"

} mark.

sayhi()

sam.

sayhi()

}

執行結果:

hi, i am mark you can call me on 222

-222

-yyyy

hi, i am sam, i work at golang inc. call me on 111

-888

-***x

Go學習 8 方法

接收者 接收者型別 func t type methodname parameter list 相當於給接受者t新增了函式methodname,類似於js中的物件導向,給類新增方法,只不過go沒有物件導向,所以這麼搞 這樣的話,呼叫methodname就需要t.methodname 來呼叫了pack...

Java學習筆記(10) 方法

方法 什麼是方法 乙個演算法邏輯功能的封裝,是一般完成乙個業務功能,如 登陸系統,建立聯絡人,簡單說 方法是動作,是動詞。方法名 一般按照方法實現的功能定名,一般使用動詞定義,如 login createcontact createanswer 方法引數 是方法的前提條件,是方法執行依據,是資料,如...

Go系列(二) 方法

方法宣告 package geometry import math type point struct type path point 函式 func distance p,q point float64 方法 p 方法的接收器 func p point distance q point float...